Post on 19-Jul-2020
I111E Algorithms & Data Structures8. Sorting(1):
Bubble, insertion, and heap Sort
1
School of Information ScienceRyuhei Uehara & Giovanni Vigliettauehara@jaist.ac.jp & johnny@jaist.ac.jp
2019-11-13All materials are available at
http://www.jaist.ac.jp/~uehara/couse/2019/i111e
C Version
65 12 46 97 56 33 75 53 21 Input12 21 33 46 53 56 65 75 93 Increasing93 75 65 56 53 46 33 21 12 Decreasing
Sorting
• Make given set of data in order– Numerical data: increasing/decreasing order
– String data: lexicographical orderinge.g., aaa, aab, aba, abb, baa, bab, bbc, bcb
• Tons of sorting algorithms– Bubble sort, insertion sort, heap sort, merge sort,
quick sort, and counting sort, and so on…
2See the volume 3 of TAOCP by D. Knuth for more algorithms…
BUBBLE SORT
3
Bubble sort• From left to right, if they are not in order,
swap them65 12 46 97 56 33 75 53 21
12 65 46 97 56 33 75 53 21
12 46 65 97 56 33 75 53 21
12 46 65 56 97 33 75 53 21
It is not yet sorted completely….
4
65 12 46 97 56 33 75 53 21
12 65 56 97
46 65 33 97
75 97
53 97
21 97
Bubble sort: 1st sweep
12 46 65 56 33 75 53 21 97
Maximum
5
12 46 65 56 33 75 53 21 97
56 65 53 75
33 65 21 75
Bubble sort: 2nd sweep
12 46 56 33 65 53 21 75 97
Sorted
They are not sorted yet next sweep6
65 12 46 97 56 33 75 53 21 : k=0 Input12 46 65 56 33 75 53 21 97 : k=112 46 56 33 65 53 21 75 97 : k=212 46 33 56 53 21 65 75 97 : k=312 33 46 53 21 56 65 75 97 : k=412 33 46 21 53 56 65 75 97 : k=512 33 21 46 53 56 65 75 97 : k=612 21 33 46 53 56 65 75 97 : k=712 21 33 46 53 56 65 75 97 : k=8 all sorted
Sorted area
Bubble sort: Number of sweeps
• By k sweeps, k data are sorted →the number of sweeps to sort all is n-1 times.
7
• Program
• # of comparisons:
– It takes Θ(n2) time even for sorted input to compare– For data in reverse order, the number of swaps is
also Θ(n2)
for(k=1; k<n; k=k+1)for(i=0; i<n-k; i=i+1)
if(data[i] > data[i+1])swap(&data[i], &data[i+1]);
Bubble sort: Time complexity
8
※Use swap!
Example: Implementation of Bubble Sort
9
public class i111_09_p9 {public static void Main(){
int[] data = new int[]{65,12,46,97,56,33,75,53,21};print(data);
int n = data.Length;for (int k=1; k<n; k++) {
for (int i=0; i<n-k; i++) {if (data[i]>data[i+1]) {
int t=data[i]; data[i]=data[i+1]; data[i+1]=t;}
}print(data);
}}
public static void print(int[] ar) {for (int i=0; i<ar.Length; i++) System.Console.Write(ar[i]+" ");System.Console.WriteLine();
}}
swap
Initialize data
Output
Body←※
INSERTION SORT
10
We need to shift the elementslarger than the current item
65 12 46 97 56 33 75 53 21
12 65 46
12 46 65 97
12 46 65 97 56
12 46 56 65 97 33
12 33 46 56 65 97 75
12 33 46 56 65 75 97 53
12 33 46 53 56 65 75 97 21
12 21 33 46 53 56 65 75 97
Insertion sort
• Sorted elements grows one by one3 5 8 10 12 6
for(i=1; i<n; i=i+1){x = data[i];j=i;while(data[j-1]>x && j>0){
data[j] = data[j-1];j=j-1;
}data[j] = x;
}
11
Until smaller than x
Move larger one than x to right
Insertion sort: time complexity
• Best case: Θ(n)– When input data were already sorted
• Worst case: Θ(n2)– When data are in reverse order– Move all items for each item
• On average: Θ(n2)– When the data is the k-th one among m sorted
data; In this case, we need k comparisons
12
HEAP SORT
13
Heap sort
• Data structure heap– Insertion of data: Θ(log n) time– Take the maximum element: Θ(log n) time
• How to sort by heap– Step 1: Put n elements into heap– Step 2: Repeat to take the maximum element
from heap, and copy it to the rightmost element• Computational Complexity:
– Both of steps 1 and 2 take Θ(n log n) time.
14
65
(1)add 65
65
(2)add 12
12
65
(3)add 46
12 46
65
(4)add 97
12 46
97
97
65 46
12
97
65 46
12 56
(5)add 56
97
65 46
12 56
(6)add 33
33
… in the same way, we can adddata to heap one by one:
Example of heap sort @Step 1Data = 65 12 46 97 56 33 75 53 21
1 2 3 4 5 6 7 8 9
97 65 75 53 56 33 46 12 21
15
97
65 75
53 56 33 46
12 21
1
2 3
4 5 6 7
8 9
21
65 75
53 56 33 46
12
1
2 3
4 5 6 7
8
75
65 46
53 56 33 21
12
1
2 3
4 5 6 7
8
Take the maximum
Example of heap sort @Step 2
Fix heap
75 65 46 53 56 33 21 12
Copy to the rightmost element
75 65 46 53 56 33 21 12 97
Note: We can sort them “in place”.
97
65 75
53 56 33 46
12 21
21
65 75
53 56 33 46
12 97
(1)delete max (97)75
65 46
53 56 33 21
12 97
12
65 46
53 56 33 21
97
(2) delete max (75)
75
65
56 46
53 12 33 21
Example of heap sort @Step 2array = :
75 65 46 53 56 33 21 12 97
65 56 46 53 12 33 21 75 97
97 65 75 53 56 33 46 12 21
17
Example: Implementation of heap(We use an extra array and copy data from smallest one)
18
public class i111_09_p23 {public static void Main(){
int[] data = new int[]{65,12,46,97,56,33,75,53,21};
print(data);
int n = data.Length;HeapArray heap = new HeapArray(n);for (int i=0; i<n; i++) heap.push(data[i]);for (int i=0; i<n; i++) data[i] = heap.pop();print(data);
}
public static void print(int[] ar) {for (int i=0; i<ar.Length; i++)
System.Console.Write(ar[i]+" ");System.Console.WriteLine();
}}
Body
Put them all, and take them all
Implementation of
HeapArray
19
class HeapArray {private int[] heap;private int n;public HeapArray(int size) {
heap = new int[size];n = 0;
}
public void push(int x){heap[n] = x; int child = n;int parent = (n-1) /2; while (child != 0 && x<heap[parent]) {
heap[child] = heap[parent];child = parent;parent = (child-1)/2;
}heap[child] = x;n++;
}
public int pop() {int minValue = heap[0];heap[0] = heap[n-1];n--;int parent = 0;int child = parent*2+1;while (child <n) {
if (child+1<n && heap[child]>heap[child+1]) child++; if (heap[parent]<=heap[child]) break;int t=heap[child]; heap[child]=heap[parent]; heap[parent]=t;parent = child;child = parent*2+1;
}return minValue;
}}
In this implementation,parent is smaller than children
←※
←※