4.15 Sorting Algorithms

3 阅读3分钟

1. Exam Points

  • Implement selection sort on arrays or ArrayLists.
  • Implement insertion sort on arrays or ArrayLists.
  • Predict current elements after some certain rounds of selection or insertion.
  • Analyze how many times comparing and swapping are executed (selection sorting).
    • depends on the data given, analyze the result of each round.
    • number of rounds: length - 1
    • number of comparisons: n(n-1)/2
  • Analyze how many times moving and insertion are executed (insertion sorting).
    • depends on the data given, analyze the result of each round.
    • trick: after j+1 passes, the elements with index 0 to j are ordered.

2. Knowledge Points

(1) Implement Sorting Algorithms

  • Selection sort and insertion sort are iterative sorting algorithms that can be used to sort elements in an array(1D or 2D) or ArrayList.
    • selection sort: swap the smallest or largest to its correct position.
    • insertion sort: insert one element into its correct position.

(2) Selection Sort

  • Selection sort: Selection sort repeatedly selects the smallest(or largest) element from the unsorted portion of the list and swaps it into its correct (and final) position in the sorted portion of the list.
  • 选择排序-每次选择未排序区域中最小或最大的值,将其和排序区域中对应位置元素交换。
  • Demonstration: 1 round of sorting
    image.png
    image.png
  • Step-by-step analysis:
    • original data: 4, 3, 5, 2, 1
      • 1st round: swap 1 (current smallest among all) into the 1st place, and you get 1, 3, 5, 2, 4
      • 2st round: swap 2 (current smallest among the last 4) into the 2nd place, and you get 1, 2, 5, 3, 4
      • 3rd round: swap 3 (current smallest among the last 3) into the 3rd place, and you get 1, 2, 3, 5, 4
      • 4th round: swap 4 (current smallest among the last 2) into the 4th place, and you get 1, 2, 3, 4, 5
  • Implementation Code:
        int[] nums = { 4, 3, 5, 2, 1 };
    	
        for (int i = 0; i < nums.length - 1; i++) { // totally length-1 rounds
    
            // i is the correct position for swapping
            
            // set the index of the minimum value for the current round
            int minIndex = i; 
    
            // traverse the elements after i, and find the index of the minimum
            for (int j = i + 1; j < nums.length; j++) {
            
                // if a lesser value is found, update the index of the minimum value
                if (nums[j] < nums[minIndex]) { 
                    minIndex = j;
                }
            }
            
            // if minIndex is not i
            if (minIndex != i) { // swap the minimum to its correct position
                int temp = nums[i];
                nums[i] = nums[minIndex];
                nums[minIndex] = temp;
            }
        }
    

(3) Insertion Sort

  • Insertion sort: Insertion sort inserts an element from the unsorted portion of a list into its correct (but not necessarily final) position in the sorted portion of the list by shifting elements of the sorted portion to make room for the new element.
  • 每次将一个未排序区域的元素,插入到排序区域中对应的位置上,插入之前需要将其他元素移动,以腾出插入的空间。
  • Demonstration: 1 round of sorting
    image.png
    image.png
  • Step-by-step analysis:
    • original data: 5, 4, 3, 2, 1
      • 1st round: move 5 forward, and then insert 4 into the first place, and you get 4, 5, 3, 2, 1.
      • 2nd round: move 5, 4 forward, and then insert 3 into the first place, and you get 3, 4, 5, 2, 1.
      • 3rd round: move 5, 4, 3 forward, and then insert 2 into the first place, and you get 2, 3, 4, 5, 1.
      • 4th round: move 5, 4, 3, 2 forward, and then insert 1 into the first pace, and you get 1, 2, 3, 4, 5.
  • Implementation Code on 1d array:
        int[] nums = { 5, 4, 3, 2, 1 };
    
        // traverse the collection from the second to the last element (elements to b inserted)
        for (int i = 1; i < nums.length; i++) {
    
            // get the current element, that is, the element to be inserted for this round
            int x = nums[i];
            
            // k represents the position for insertion,
            // first set the insertion position as i 
            int k = i;
    
            // if the elements before x is greater, move them forward, and update k
            while (k > 0 && x < nums[k-1]) {
                nums[k] = nums[k - 1];
                k--;
            }
        
            // insert x into position k
            nums[k] = x;
        }
    
  • Implementation Code on ArrayLists:
    ArrayList<Integer> list = new ArrayList<Integer>();
    list.add(5);
    list.add(4);
    list.add(3);
    list.add(2);
    list.add(1);
    
    for (int i = 1; i < list.size(); i++) {
    
        int x = list.get(i);
        int k = i;
    
        while (k > 0 && x < list.get(k - 1)) {
            list.set(k, list.get(k - 1));
            k--;
        }
    
        list.set(k, x);
    }
    

3. Exercises