Lab 4
Lab 4
Lab 4
Lab 4
Sorting
Quang D. C.
[email protected]
In this tutorial, we will approach some basic sorting algorithms, i.e., Selection sort, Bubble
sort, Insertion sort, Merge sort, Quicksort; and use Java method to perform sorting.
1. Sorting Algorithms
We will focus on implementing Selection sort, Bubble sort, Insertion sort and un-
derstand the main ideas of Merge sort and Quick sort.
[email protected] 1
Ton Duc Thang University
Faculty of Information Technology Data Structures and Algorithms, Fall 2020-2021
[email protected] 2
Ton Duc Thang University
Faculty of Information Technology Data Structures and Algorithms, Fall 2020-2021
The following figure illustrates a recursive merge sort algorithm used to sort an
array of 7 integer values. These are the steps a human would take to emulate merge sort
(top-down).
[email protected] 3
Ton Duc Thang University
Faculty of Information Technology Data Structures and Algorithms, Fall 2020-2021
[email protected] 4
Ton Duc Thang University
Faculty of Information Technology Data Structures and Algorithms, Fall 2020-2021
29 j++;
30 }
31 k++;
32 }
33 // Copy remaining elements of L[] if any
34 while (i < n1)
35 {
36 arr[k] = L[i];
37 i++;
38 k++;
39 }
40 // Copy remaining elements of R[] if any
41 while (j < n2)
42 {
43 arr[k] = R[j];
44 j++;
45 k++;
46 }
47 }
48
49
50 // Main function of merge sort that sorts arr[ first .. last] using
merge () method
51 public static void mergeSort (int [] arr , int first , int last)
52 {
53 if (first < last)
54 {
55 // Find the middle point
56 int middle = ( first + last)/2;
57 // Sort first and second halves
58 mergeSort (arr , first , middle );
59 mergeSort (arr , middle + 1, last);
60 // Merge the sorted halves
61 merge (arr , first , middle , last);
62 }
63 }
64
[email protected] 5
Ton Duc Thang University
Faculty of Information Technology Data Structures and Algorithms, Fall 2020-2021
The pivot selection and partitioning steps can be done in several different ways; the choice
of specific implementation schemes greatly affects the algorithm’s performance.
The following figure illustrates the quicksort algorithm. The shaded element is the pivot.
It is always chosen as the last element of the partition. However, always choosing the last
element in the partition as the pivot in this way results in poor performance, 𝑂(𝑛2 ), on
already sorted arrays, or arrays of identical elements.
3 private static int partition (int [] arr , int low , int high)
4 {
5 int pivot = arr[high ];
6 // index of smaller element
7 int i = (low - 1);
8 for (int j = low; j < high; j++)
9 {
10 // If current element is smaller than or equal to pivot
11 if (arr[j] <= pivot )
12 {
[email protected] 6
Ton Duc Thang University
Faculty of Information Technology Data Structures and Algorithms, Fall 2020-2021
13 i++;
14 // swap arr[i] and arr[j]
15 int temp = arr[i];
16 arr[i] = arr[j];
17 arr[j] = temp;
18 }
19 }
20 // swap arr[i+1] and arr[high] (or pivot )
21 int temp = arr[i+1];
22 arr[i+1] = arr[high ];
23 arr[high] = temp;
24 return i+1;
25 }
26
27 // The main function that implements QuickSort algorithm
28 // arr [] --> Array to be sorted ,
29 // low --> Starting index ,
30 // high --> Ending index
31 public static void QuickSort (int [] arr , int low , int high)
32 {
33 if (low < high)
34 {
35 // pi is partitioning index , arr[pi] is now at right place
36 int pi = partition (arr , low , high);
37 // Recursively sort elements before partition and after
partition
38 QuickSort (arr , low , pi - 1);
39 QuickSort (arr , pi + 1, high);
40 }
41 }
[email protected] 7
Ton Duc Thang University
Faculty of Information Technology Data Structures and Algorithms, Fall 2020-2021
8 this. denom = 1;
9 }
10
11 public Fraction (int num , int denom )
12 {
13 this.num = num;
14 this. denom = denom ;
15 }
16
17 public double getRatio ()
18 {
19 return ( double ) this.num / this.denom ;
20 }
21
22 @Override
23 public String toString ()
24 {
25 return this.num + "/" + this.denom ;
26 }
27 }
[email protected] 8
Ton Duc Thang University
Faculty of Information Technology Data Structures and Algorithms, Fall 2020-2021
3. Exercise
Exercise 1
Implement all sorting algorithms presented in this tutorial. (focus on implementing
code of Selection sort, Bubble sort, Insertion sort from their pseudocode)
Exercise 2
Applying the java.util.Comparator to sort a list of Student by the average grade in
ascending and descending order. The attributes of Student: