Advertisement
pro-themes

Bubble Sort

Jun 13th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.00 KB | None | 0 0
  1. /**
  2. ** Jessica Wong
  3.  */
  4.  
  5. import java.util.*;
  6. public class Main {
  7.  
  8.     public static void main(String[] args) {
  9.         int array[] = {3,4,1,2,5};
  10.         int array2[] = {3,4,1,2,5};
  11.         int array3[] = {3,4,1,2,5};
  12.         List<Integer> list = new ArrayList<>();
  13.         for (int i = 0; i < 5; i++)
  14.         {
  15.             list.add((int)(Math.random()*10));
  16.         }
  17.         arrayPrint(array);
  18.         arraySort_bubble(array);
  19.         arrayPrint(array);
  20.         arraySort_selection(array2);
  21.         arrayPrint(array2);
  22.         arraySort_insertion(array3);
  23.         arrayPrint(array3);
  24.         System.out.println("Unsorted List: " + list);
  25.         listSort_selection(list);
  26.         System.out.println("Sorted List: " + list);
  27.     }
  28.  
  29.     public static void arraySort_bubble(int[] sort)
  30.     {
  31.         for (int x = 0; x < sort.length; x++)
  32.         {
  33.             for (int y = 0; y < sort.length-1; y++)
  34.             {
  35.                 int temp = Math.min(sort[y], sort[y+1]);
  36.                 sort[y] = Math.max(sort[y], sort[y+1]);
  37.                 sort[y+1] = temp;
  38.             }
  39.         }
  40.     }
  41.    
  42.     public static void arraySort_selection(int[] sort)
  43.     {
  44.         for (int x = 0; x < sort.length; x++)
  45.         {
  46.             int maxPos = x;
  47.             int max = sort[x];
  48.             for (int y = x+1; y < sort.length; y++)
  49.             {
  50.                 if (sort[y] > max)
  51.                 {
  52.                     max = sort[y];
  53.                     maxPos = y;
  54.                 }
  55.             }
  56.             sort[maxPos] = sort[x];
  57.             sort[x] = max;
  58.         }
  59.     }
  60.    
  61.     public static void arraySort_insertion(int[] a)
  62.     {
  63.         int itemToInsert, j;
  64.         boolean keepGoing;
  65.         //On kth pass, insert item k into its correct position among the first k items in the array
  66.         for(int k = 1; k < a.length; k++)
  67.         {
  68.             //Go backwards through the list, looking for the slot to insert a[k]
  69.             itemToInsert = a[k];
  70.             j = k - 1;
  71.             keepGoing = true;
  72.             while((j >= 0) && keepGoing)
  73.             {
  74.                 if (itemToInsert > a[j] )
  75.                 {
  76.                     a[j + 1] = a[j]; //Salient feature
  77.                     j--;
  78.                     if(j == -1) //special case for inserting an item at [0]
  79.                         a[0] = itemToInsert;
  80.                 }
  81.                 else //Upon leaving loop, j + 1 is the index where itemToInsert belongs
  82.                 {
  83.                 keepGoing = false;
  84.                 a[j + 1] = itemToInsert;
  85.                 }
  86.             }
  87.         }
  88.     }
  89.     public static void arrayPrint(int[] arr)
  90.     {
  91.         for (int x = 0; x < arr.length; x++)
  92.         {
  93.             if (x == arr.length-1)
  94.             {
  95.                 System.out.println(arr[x]);
  96.             }
  97.             else
  98.             {
  99.                 System.out.print(arr[x] + ", ");
  100.             }
  101.         }
  102.     }
  103.    
  104.     public static void listSort_selection(List<Integer> num)
  105.     {
  106.         for (int x = 0; x < num.size(); x++)
  107.         {
  108.             int minPos = x;
  109.             int min = num.get(x);
  110.             for (int y = x+1; y < num.size(); y++)
  111.             {
  112.                 if (num.get(y) < min)
  113.                 {
  114.                     min = num.get(y);
  115.                     minPos = y;
  116.                 }
  117.             }
  118.             num.set(minPos, num.get(x));
  119.             num.set(x, min);
  120.         }
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement