Advertisement
Guest User

SortTest.java with ALL SORTS COMPLETE

a guest
Mar 29th, 2017
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.20 KB | None | 0 0
  1. public class SortTest{
  2.  
  3.   public static void main(String args[]){
  4.     int theArray[] = {4,2,5,1,3,18,0,9,6};
  5.     //selectionSort(theArray);
  6.     //insertionSort(theArray);
  7.     mergeSort(theArray, 0, theArray.length - 1);
  8.     for(int j = 0; j < theArray.length; j++){
  9.       System.out.print(theArray[j] + " ");
  10.     }
  11.     System.out.println(" ");
  12.   }
  13.  
  14.   public static void selectionSort(int a[ ]){
  15.     int minIndex, min;
  16.     for(int i = 0; i < a.length - 1; i++){//keeps track of swap position
  17.       minIndex = i;
  18.       min = a[i];
  19.       for(int j = i + 1; j < a.length; j++){//finds the minIndex
  20.         if(a[j] < min){
  21.           minIndex = j;
  22.           min = a[j];
  23.         }
  24.       }
  25.       //swap after finding min
  26.       int temp = a[i];
  27.       a[i] = a[minIndex];
  28.       a[minIndex] = temp;
  29.     }
  30.   }
  31.  
  32.   public static void insertionSort(int a[ ]){
  33.     int itemToInsert, j;
  34.     boolean keepGoing;
  35.     //On kth pass, insert item k into its correct position
  36.     //among the first k elements in the array
  37.     for(int k = 1; k < a.length; k++){
  38.       //Go backwards through the sorted part of the array
  39.       //looking for the correct spot for the kth element
  40.       itemToInsert = a[k];
  41.       j = k - 1;
  42.       keepGoing = true;
  43.       while( j >= 0 && keepGoing == true ){
  44.         if(itemToInsert < a[j]){
  45.           //If element is less than a[j], shift to the right to make room
  46.           a[j+1] = a[j];
  47.           j--;
  48.           if( j == -1 ){//If we are at the leftmost element, insert
  49.             a[0] = itemToInsert;
  50.           }
  51.         }
  52.         else{//The correct place for the element is found, insert
  53.           keepGoing = false;
  54.           a[j+1] = itemToInsert;
  55.         }
  56.       }
  57.     }
  58.   }
  59.  
  60.   //This does the initial splitting step
  61.   public static void mergeSort(int a[ ], int left, int right){
  62.     if(right == left) return;
  63.     int middle = (left + right) / 2;
  64.     mergeSort(a, left, middle);//recursion (left half)
  65.     mergeSort(a, middle + 1, right);//recursion (right half)
  66.     merge(a, left, middle, right);//seen soon
  67.   }
  68.  
  69.   //This does the final step of merging
  70.   public static void merge(int a[], int left, int middle, int right){
  71.     //Temporary array to build the merged list
  72.     int tempArr[] = new int[right - left + 1];
  73.    
  74.     int index1 = left;
  75.     int index2 = middle + 1;
  76.     int indx = 0;
  77.    
  78.     //Loop until one of the sublists is finished, adding the smaller
  79.     //of the first elements of each sublist to the merged list
  80.     while(index1 <= middle && index2 <= right){
  81.       if(a[index1] < a[index2]){
  82.         tempArr[indx] = a[index1];
  83.         index1++;
  84.       }
  85.       else{
  86.         tempArr[indx] = a[index2];
  87.         index2++;
  88.       }
  89.       indx++;
  90.     }
  91.    
  92.     //Add to the merged "list" the remaining elements of whichever
  93.     //"sublist" is not yet finished
  94.     while( index1 <= middle ){
  95.       tempArr[indx] = a[index1];
  96.       index1++;
  97.       indx++;
  98.     }
  99.     while( index2 <= right){
  100.       tempArr[indx] = a[index2];
  101.       index2++;
  102.       indx++;
  103.     }
  104.    
  105.     //Copy the merged list from the tempArr into the a array
  106.     for( indx = 0; indx < tempArr.length; indx++ ){
  107.       a[left + indx ] = tempArr[indx];
  108.     }
  109.    
  110.   }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement