Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.72 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. public class Sorting {
  4.  
  5.     public static void main(String[] args) {
  6.         ArrayList<Integer> array = new ArrayList<Integer>();
  7.         array.add(9);
  8.         array.add(8);
  9.         array.add(7);
  10.         array.add(6);
  11.         array.add(5);
  12.         array.add(4);
  13.         array.add(3);
  14.         array.add(2);
  15.         array.add(1);
  16.         array.add(17);
  17.         array.add(18);
  18.        
  19.         ArrayList<Integer> array2 = new ArrayList<Integer>();
  20.         array2.add(13);
  21.         array2.add(6);
  22.         array2.add(0);
  23.         array2.add(6);
  24.         array2.add(34);
  25.         array2.add(77);
  26.         array2.add(3);
  27.         array2.add(2);
  28.         array2.add(111);
  29.         array2.add(17);
  30.         array2.add(18);
  31.  
  32.         //Bubble Sort
  33.        
  34.         //Starts a loop at 0, goes to size - 2 to avoid trying to access an index which doesn't exist
  35.         for(int i=0; i<= array.size()-2; i++){
  36.             //creating k to effectively have 2 i's to cover if statement later on
  37.             int k = i;
  38.            
  39.             //starts a nested loop. Can have i+1 and < array.size() because of -2 in parent loop
  40.             for(int j= i+1; j< array.size(); j+=1){
  41.                 //if number in smaller index is bigger, save k as j because we gotta deal with that
  42.                 if(array.get(k)> array.get(j))
  43.                     k = j;
  44.             }
  45.            
  46.             //gets the index of k and stores it as temporary
  47.             int temporary =  array.get(k);
  48.             //swaps the contents of indexes k and i, but if the above if
  49.             //statement returns false, the values are the same, so it just does nothing
  50.              array.set(k,array.get(i));
  51.              array.set(i, temporary);
  52.              
  53.              
  54.             }
  55.             //prints out ArrayList to make sure sorting was done correctly
  56.         for(int i=0; i< array.size(); i++)
  57.             System.out.print(array.get(i)+", ");
  58.        
  59.         System.out.println();
  60.        
  61.         //Selection Sort
  62.        
  63.         //Start a loop to iterate through array
  64.         for(int i=0; i< array2.size(); i++){
  65.             //we need a minimum, so it makes sense to start with it as the first element of the array
  66.            int minimum = array2.get(i);
  67.            //we need to store the index of the minimum, so its going to be temp
  68.            int temp = i;
  69.            //loop to search for a new minimum with every element
  70.            for (int j = i+1; j < array2.size(); j++) {
  71.                //if statement to find out if the proceeding element is a new minimum
  72.                if (array2.get(j) < minimum) {
  73.                    //we found a new minimum! set it appropriately
  74.                    minimum = array2.get(j);
  75.                    //update the index of the new minimum as well
  76.                    temp = j;
  77.                }
  78.            }
  79.            //once again, if the if statement above returns false, this does nothing,
  80.            //but if it returns true, swap the contents of these indexes
  81.            array2.set(temp, array2.get(i));
  82.            array2.set(i, minimum);
  83.              
  84.              
  85.             }
  86.  
  87.         for(int i=0; i< array2.size(); i++) {
  88.             System.out.print( array2.get(i)+", ");
  89.        
  90.     }
  91.        
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement