Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MYSORT
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package collection;
- /**
- *
- * @author James
- */
- public class MySort {
- private MySort() {} // Prevents Instantiation
- /*
- *
- */
- public static void swap(MyVector vec, int first, int second)
- {
- Object temp = vec.elementAt(first);
- vec.replace(first, vec.elementAt(second));
- vec.replace(second, temp);
- }
- /*
- *
- */
- public static MyVector bubbleSort(MyVector vec)
- {
- int i,
- j,
- n = vec.size();
- Comparable first,
- second;
- for (i = 1; i < n; i++)
- for (j = n - 1; j >= i; j--)
- {
- first = (Comparable)vec.elementAt(j-1);
- second = (Comparable)vec.elementAt(j);
- if (first.compareTo(second) > 0)
- swap(vec, j - 1, j);
- }
- return vec;
- }
- /*
- *
- */
- public static MyVector selectionSort(MyVector vec)
- {
- int i,j, n = vec.size();
- int smallPos;
- Comparable smallest, current;
- for (i = 0; i < n - 1; ++i)
- {
- smallPos = i;
- smallest = (Comparable)vec.elementAt(i);
- for (j = i + 1; j < n; ++j)
- {
- current = (Comparable)vec.elementAt(j);
- if (current.compareTo(smallest) < 0)
- {
- smallPos = j;
- smallest = current;
- }
- }
- swap(vec, i, smallPos);
- }
- return vec;
- }
- }
- ------------- LAB CLASS
- public class Lab3 {
- public static void test()
- {
- MyVector numVec = new MyVector(61);
- Random r = new Random();
- r.setSeed(java.lang.System.nanoTime());
- for (int i = 0; i < 61; i++)
- {
- if (i % 5 == 0) { System.out.print("\n"); }
- int data = new Integer(100 + r.nextInt(900));
- numVec.insertAt(i, data);
- System.out.print("Index:" + i + ": " + numVec.elementAt(i) + "\t");
- }
- MySort.bubbleSort(numVec);
- for (int i = 0; i < 61; i++)
- {
- if (i % 5 == 0) { System.out.print("\n"); }
- System.out.print("Index:" + i + ": " + numVec.elementAt(i) + "\t");
- }
- }
- }
Add Comment
Please, Sign In to add comment