Advertisement
ridjis

Sort Algorithms

Nov 11th, 2014
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.33 KB | None | 0 0
  1. public class ArrayStructures {
  2.        
  3.     private int[] theArray = new int[50];
  4.    
  5.     private int arraySize = 10;
  6.    
  7.     public void generateRandomArray(){
  8.        
  9.         for(int i = 0; i < arraySize; i++){
  10.            
  11.             theArray[i] = (int)(Math.random()*10)+10;
  12.            
  13.         }
  14.        
  15.     }
  16.    
  17.     public void printArray(){
  18.        
  19.         System.out.println("----------");
  20.         for(int i = 0; i < arraySize; i++){
  21.            
  22.             System.out.print("| " + i + " | ");
  23.             System.out.println(theArray[i] + " |");
  24.            
  25.             System.out.println("----------");
  26.            
  27.         }
  28.        
  29.     }
  30.    
  31.     public int getValueAtIndex(int index){
  32.        
  33.         if(index < arraySize) return theArray[index];
  34.        
  35.         return 0;
  36.        
  37.     }
  38.    
  39.     public boolean doesArrayContainThisValue(int searchValue){
  40.        
  41.         boolean valueInArray = false;
  42.        
  43.         for(int i = 0; i < arraySize; i++){
  44.            
  45.             if(theArray[i] == searchValue){
  46.                
  47.                 valueInArray = true;
  48.                
  49.             }
  50.            
  51.         }
  52.        
  53.         return valueInArray;
  54.        
  55.     }
  56.    
  57.     public void deleteIndex(int index){
  58.        
  59.         if(index < arraySize){
  60.            
  61.             for(int i = index; i < (arraySize - 1); i++){
  62.                
  63.                 theArray[i] = theArray[i+1];
  64.                
  65.             }
  66.            
  67.             arraySize--;
  68.            
  69.         }
  70.        
  71.     }
  72.    
  73.     public void insertValue(int value){
  74.        
  75.         if(arraySize < 50){
  76.            
  77.             theArray[arraySize] = value;
  78.            
  79.             arraySize++;
  80.            
  81.         }
  82.        
  83.     }
  84.    
  85.     public String linearSearchForValue(int value){
  86.        
  87.         boolean valueInArray = false;
  88.        
  89.         String indexsWithValue = "";
  90.        
  91.         for(int i = 0; i < arraySize; i++){
  92.            
  93.             if(theArray[i] == value){
  94.                
  95.                 valueInArray = true;
  96.                
  97.                 indexsWithValue+= i + " ";
  98.                
  99.             }
  100.            
  101.             printHorzArray(i, -1);
  102.            
  103.         }
  104.        
  105.         if(!valueInArray){
  106.            
  107.             indexsWithValue = "None";
  108.            
  109.         }
  110.        
  111.         System.out.print("The Value was Found in the Following: " + indexsWithValue);
  112.        
  113.         System.out.println();
  114.        
  115.         return indexsWithValue;
  116.        
  117.     }
  118.    
  119. public void printHorzArray(int i, int j){
  120.        
  121.         for(int n = 0; n < 51; n++)System.out.print("-");
  122.        
  123.         System.out.println();
  124.        
  125.         for(int n = 0; n < arraySize; n++){
  126.            
  127.             System.out.print("| " + n + "  ");
  128.            
  129.         }
  130.        
  131.         System.out.println("|");
  132.        
  133.         for(int n = 0; n < 51; n++)System.out.print("-");
  134.        
  135.         System.out.println();
  136.        
  137.         for(int n = 0; n < arraySize; n++){
  138.            
  139.             System.out.print("| " + theArray[n] + " ");
  140.            
  141.         }
  142.        
  143.         System.out.println("|");
  144.        
  145.         for(int n = 0; n < 51; n++)System.out.print("-");
  146.        
  147.         System.out.println();
  148.        
  149.         // END OF FIRST PART
  150.        
  151.        
  152.         // ADDED FOR BUBBLE SORT
  153.        
  154.         if(j != -1){
  155.        
  156.             // ADD THE +2 TO FIX SPACING
  157.            
  158.             for(int k = 0; k < ((j*5)+2); k++)System.out.print(" ");
  159.            
  160.             System.out.print(j);
  161.            
  162.         }
  163.        
  164.        
  165.         // THEN ADD THIS CODE
  166.        
  167.         if(i != -1){
  168.            
  169.             // ADD THE -1 TO FIX SPACING
  170.            
  171.             for(int l = 0; l < (5*(i - j)-1); l++)System.out.print(" ");
  172.            
  173.             System.out.print(i);
  174.            
  175.         }
  176.        
  177.         System.out.println();
  178.        
  179.     }
  180.    
  181.     // This bubble sort will sort everything from
  182.         // smallest to largest
  183.        
  184.         public void bubbleSort(){
  185.            
  186.             // i starts at the end of the Array
  187.             // As it is decremented all indexes greater
  188.             // then it are sorted
  189.            
  190.             for(int i = arraySize - 1; i > 1; i--){
  191.                
  192.                 // The inner loop starts at the beginning of
  193.                 // the array and compares each value next to each
  194.                 // other. If the value is greater then they are
  195.                 // swapped
  196.                
  197.                 for(int j = 0; j < i; j++){
  198.                    
  199.                     // To change sort to Descending change to <
  200.                    
  201.                     if(theArray[j] > theArray[j + 1]){
  202.                        
  203.                         swapValues(j, j+1);
  204.                        
  205.                         printHorzArray(i, j);
  206.                        
  207.                     }
  208.                    
  209.                 }
  210.                
  211.             }
  212.            
  213.         }
  214.        
  215.         public void swapValues(int indexOne, int indexTwo){
  216.            
  217.             int temp = theArray[indexOne];
  218.             theArray[indexOne] = theArray[indexTwo];
  219.             theArray[indexTwo] = temp;
  220.            
  221.         }
  222.        
  223.        
  224.         // The Binary Search is quicker than the linear search
  225.         // because all the values are sorted. Because everything
  226.         // is sorted once you get to a number larger than what
  227.         // you are looking for you can stop the search. Also
  228.         // you be able to start searching from the middle
  229.         // which speeds the search. It also works best when
  230.         // there are no duplicates
  231.        
  232.         public void binarySearchForValue(int value){
  233.            
  234.             int lowIndex = 0;
  235.             int highIndex = arraySize - 1;
  236.            
  237.             while(lowIndex <= highIndex){
  238.                
  239.                 int middleIndex = (highIndex + lowIndex) / 2;
  240.                
  241.                 if(theArray[middleIndex] < value) lowIndex = middleIndex + 1;
  242.                
  243.                 else if(theArray[middleIndex] > value) highIndex = middleIndex - 1;
  244.                
  245.                 else {
  246.                    
  247.                     System.out.println("\nFound a Match for " + value + " at Index " + middleIndex);
  248.                    
  249.                     lowIndex = highIndex + 1;
  250.                    
  251.                 }
  252.                
  253.                 printHorzArray(middleIndex, -1);
  254.                
  255.             }
  256.            
  257.         }
  258.        
  259.         // Selection sort search for the smallest number in the array
  260.         // saves it in the minimum spot and then repeats searching
  261.         // through the entire array each time
  262.        
  263.         public void selectionSort(){
  264.            
  265.             for(int x=0; x < arraySize; x++){
  266.                   int minimum = x;
  267.                  
  268.                   for(int y=x; y < arraySize; y++){
  269.                  
  270.                       // To change direction of sort just change
  271.                       // this from > to <
  272.                      
  273.                       if(theArray[minimum]>theArray[y]){
  274.                           minimum = y;
  275.                       }
  276.                   }
  277.                  
  278.                   swapValues(x, minimum);
  279.                  
  280.                   printHorzArray(x, -1);
  281.             }
  282.            
  283.         }
  284.        
  285.         // The Insertion Sort is normally the best of
  286.         // the elementary sorts. Unlike the other sorts that
  287.         // had a group sorted at any given time, groups are
  288.         // only partially sorted here.
  289.        
  290.         public void insertionSort(){
  291.            
  292.             for (int i = 1; i < arraySize; i++){
  293.                   int j = i;
  294.                   int toInsert = theArray[i];
  295.                   while ((j > 0) && (theArray[j-1] > toInsert)){
  296.                       theArray[j] = theArray[j-1];
  297.                       j--;
  298.                      
  299.                       printHorzArray(i, j);
  300.                      
  301.                   }
  302.                   theArray[j] = toInsert;
  303.                  
  304.                   printHorzArray(i, j);
  305.                  
  306.                   System.out.println("\nArray[i] = " + theArray[i] + " Array[j] = " + theArray[j] + " toInsert = " + toInsert + "\n");
  307.                  
  308.             }
  309.            
  310.         }
  311.    
  312.     public static void main(String[] args){
  313.        
  314.         ArrayStructures newArray = new ArrayStructures();
  315.        
  316.         newArray.generateRandomArray();
  317.        
  318.         newArray.printHorzArray(-1,-1);
  319.        
  320.         // newArray.linearSearchForValue(10);
  321.        
  322.         // newArray.bubbleSort();
  323.        
  324.         // We must Sort first
  325.        
  326.         // newArray.binarySearchForValue(17);
  327.        
  328.         // newArray.selectionSort();
  329.        
  330.         newArray.insertionSort();
  331.        
  332.     }
  333.  
  334. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement