7/31/2019 data structure notes for computer engineering note no:2
1/63
Sorting
Sorting is the process of arranging a set of
elements in a specific order.
Sorting can be classified in to
Internal sorting
External sorting
7/31/2019 data structure notes for computer engineering note no:2
2/63
Internal
All of the data are
held in the primarymemory during the
sorting process.
External
Uses primary
memory for the datacurrently being
sorted and
secondary storage
for any data thatdoes not fit in
primary memory.
Classification
7/31/2019 data structure notes for computer engineering note no:2
3/63
Bubble Sort
Traverse a collection of elements
Move from the front to the end
Bubble the largest value to the end using pair-
wise comparisons and swapping
512354277 101
1 2 3 4 5 6
7/31/2019 data structure notes for computer engineering note no:2
4/63
512354277 101
1 2 3 4 5 6
Swap
42 77
512357742 101
1 2 3 4 5 6
35 77
512773542 101
1 2 3 4 5 6
Swap
12 77
577123542 101
1 2 3 4 5 6
No need to swap
7/31/2019 data structure notes for computer engineering note no:2
5/63
577123542 101
1 2 3 4 5 6
Swap
5 101
77123542 5
1 2 3 4 5 6
101
Largest value correctly placed
7/31/2019 data structure notes for computer engineering note no:2
6/63
Notice that only the largest value is correctly
placed
All other values are still out of order
So we need to repeat this process
77123542 5
1 2 3 4 5 6
101
Largest value correctly placed
7/31/2019 data structure notes for computer engineering note no:2
7/63
The Algorithm
index A[index + 1]) then
Swap(A[index], A[index + 1])
end ifindex
7/31/2019 data structure notes for computer engineering note no:2
8/63
7/31/2019 data structure notes for computer engineering note no:2
9/63
Insertion sort eg:
7/31/2019 data structure notes for computer engineering note no:2
10/63
The Algorithm
for i=2 to n do
begin
j := i
while (a[j] < a[j-1])
begin
swap ( a[j],a[j-1] )
j = j +1
end
endComplexity of insertion sort:
In best case= 0(n);worst case=average case=0(n2)
7/31/2019 data structure notes for computer engineering note no:2
11/63
Selection Sort
The smallest element is selected from the
unsorted sublist and exchange with the
element at thebeginning of the unsorted list.
7/31/2019 data structure notes for computer engineering note no:2
12/63
Algorithm
set current to 0
loop (until last element sorted)
set smallest to current
set index to current + 1
loop (index < last)
if (index key < smallest key)set smallest to index
increment index
end loop
smallest selected; exchange with current element.
exchange (current, smallest)increment current
end loop
End selection Sort
7/31/2019 data structure notes for computer engineering note no:2
13/63
Selection Sort
Original list23 78 45 8 32 56
Unsorted
After pass 18 78 45 23 32 56
UnsortedSorted
After pass 28 23 45 78 32 56
UnsortedSorted
After pass 38 23 32 78 45 56
UnsortedSorted
After pass 48 23 32 45 78 56
UnsortedSorted
After pass 58 23 32 45 56 78
Sorted
7/31/2019 data structure notes for computer engineering note no:2
14/63
Quicksort
To sort a[left...right]:if left < right:
Partition a[left...right] such that:all a[left...p-1] are less than a[p], and
all a[p+1...right] are >= a[p]
Quicksort a[left...p-1]
Quicksort a[p+1...right]
Terminate
7/31/2019 data structure notes for computer engineering note no:2
15/63
A key step in the Quicksort algorithm ispartitioning the array
We choose some (any) numberpin the arrayto use as a pivot
We partition the array into three parts:
p
numbers
less thanp
numbers greater
than or equal top
p
7/31/2019 data structure notes for computer engineering note no:2
16/63
Choose an array value (say, the first) to use asthe pivot
Starting from the left end, find the firstelement that is greater than or equal to thepivot
Searching backward from the right end, findthe first element that is less than the pivot
Interchange (swap) these two elements Repeat, searching from where we left off, until
done
Quicksort is usuallyO(n log2n)
7/31/2019 data structure notes for computer engineering note no:2
17/63
To partition a[left...right]:
Set p = a[left], l = left + 1, r = right;
while l < r, do
while l < right & a[l] < p, set l = l + 1while r > left & a[r] >= p, set r = r - 1if l < r, swap a[l] and a[r]
Set a[left] = a[r], a[r] = p
Terminate
7/31/2019 data structure notes for computer engineering note no:2
18/63
Mergesort
A divide-and-conquer algorithm: Divide the unsorted array into 2 halves until
the sub-arrays only contain one element
Merge the sub-problem solutions together:
Compare the sub-arrays first elements
Remove the smallest element and put it into theresult array
Continue the process until all elements have beenput into the result array
37 23 6 89 15 12 2 19
7/31/2019 data structure notes for computer engineering note no:2
19/63
674523 14 6 3398 42
7/31/2019 data structure notes for computer engineering note no:2
20/63
674523 14 6 3398 42
674523 14 6 3398 42
7/31/2019 data structure notes for computer engineering note no:2
21/63
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
7/31/2019 data structure notes for computer engineering note no:2
22/63
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398
7/31/2019 data structure notes for computer engineering note no:2
23/63
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398
Merge
7/31/2019 data structure notes for computer engineering note no:2
24/63
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398
23
Merge
7/31/2019 data structure notes for computer engineering note no:2
25/63
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398
23 98
Merge
7/31/2019 data structure notes for computer engineering note no:2
26/63
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
23 98
7/31/2019 data structure notes for computer engineering note no:2
27/63
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
Merge
23 98
7/31/2019 data structure notes for computer engineering note no:2
28/63
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
14
Merge
23 98
7/31/2019 data structure notes for computer engineering note no:2
29/63
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
45
Merge
23 98 14
7/31/2019 data structure notes for computer engineering note no:2
30/63
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
Merge
98 451423
7/31/2019 data structure notes for computer engineering note no:2
31/63
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
Merge
98 14
14
23 45
7/31/2019 data structure notes for computer engineering note no:2
32/63
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
Merge
23 14
14 23
98 45
7/31/2019 data structure notes for computer engineering note no:2
33/63
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
Merge
23 98 4514
14 23 45
7/31/2019 data structure notes for computer engineering note no:2
34/63
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
Merge
23 98 4514
14 23 45 98
7/31/2019 data structure notes for computer engineering note no:2
35/63
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
23 98 4514
14 23 45 98
7/31/2019 data structure notes for computer engineering note no:2
36/63
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676
23 98 4514
14 23 45 98
7/31/2019 data structure notes for computer engineering note no:2
37/63
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676
Merge
23 98 4514
14 23 45 98
7/31/2019 data structure notes for computer engineering note no:2
38/63
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676
6
Merge
23 98 4514
14 23 45 98
7/31/2019 data structure notes for computer engineering note no:2
39/63
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676
67
Merge
23 98 4514 6
14 23 45 98
7/31/2019 data structure notes for computer engineering note no:2
40/63
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676 33 42
23 98 4514 676
14 23 45 98
7/31/2019 data structure notes for computer engineering note no:2
41/63
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676 33 42
Merge
23 98 4514 676
14 23 45 98
7/31/2019 data structure notes for computer engineering note no:2
42/63
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676 33 42
Merge
3323 98 4514 676
14 23 45 98
7/31/2019 data structure notes for computer engineering note no:2
43/63
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676 33 42
Merge
4223 98 4514 676 33
14 23 45 98
7/31/2019 data structure notes for computer engineering note no:2
44/63
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676 33 42
Merge
23 98 4514 676 4233
14 23 45 98
7/31/2019 data structure notes for computer engineering note no:2
45/63
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676 33 42
Merge
23 98 4514 6 4233
14 23 45 98 6
67
7/31/2019 data structure notes for computer engineering note no:2
46/63
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676 33 42
Merge
23 98 4514 6 33
14 23 45 98 6 33
67 42
7/31/2019 data structure notes for computer engineering note no:2
47/63
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676 33 42
Merge
23 98 4514 6 4233
14 23 45 98 6 33 42
67
7/31/2019 data structure notes for computer engineering note no:2
48/63
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676 33 42
Merge
23 98 4514 676 4233
14 23 45 98 6 33 42 67
7/31/2019 data structure notes for computer engineering note no:2
49/63
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676 33 42
Merge
23 98 4514 676 4233
23 45 98 33 42 6714 6
7/31/2019 data structure notes for computer engineering note no:2
50/63
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676 33 42
Merge
23 98 4514 676 4233
23 45 98 6 42 67
6
14 33
7/31/2019 data structure notes for computer engineering note no:2
51/63
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676 33 42
Merge
23 98 4514 676 4233
14 45 98 6 42 67
6 14
23 33
7/31/2019 data structure notes for computer engineering note no:2
52/63
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676 33 42
Merge
23 98 4514 676 4233
14 23 98 6 42 67
6 14 23
45 33
7/31/2019 data structure notes for computer engineering note no:2
53/63
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676 33 42
Merge
23 98 4514 676 4233
14 23 98 6 33 67
6 14 23 33
45 42
7/31/2019 data structure notes for computer engineering note no:2
54/63
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676 33 42
Merge
23 98 4514 676 4233
14 23 98 6 33 42
6 14 23 33 42
45 67
7/31/2019 data structure notes for computer engineering note no:2
55/63
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676 33 42
Merge
23 98 4514 676 4233
14 23 45 6 33 42
6 14 23 33 42 45
98 67
7/31/2019 data structure notes for computer engineering note no:2
56/63
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676 33 42
Merge
23 98 4514 676 4233
14 23 45 98 6 33 42 67
6 14 23 33 42 45 67
7/31/2019 data structure notes for computer engineering note no:2
57/63
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676 33 42
Merge
23 98 4514 676 4233
14 23 45 98 6 33 42 67
6 14 23 33 42 45 67 98
7/31/2019 data structure notes for computer engineering note no:2
58/63
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676 33 42
23 98 4514 676 4233
14 23 45 98 6 33 42 67
6 14 23 33 42 45 67 98
7/31/2019 data structure notes for computer engineering note no:2
59/63
674523 14 6 3398 42
6 14 23 33 42 45 67 98
7/31/2019 data structure notes for computer engineering note no:2
60/63
Heap Sort
It is an improved version of selection sort in
which the largest element is selected and
exchanged with the last element in the
unsorted list.
7/31/2019 data structure notes for computer engineering note no:2
61/63
set index to 1loop (heap built)
reheapUp (heap, index)
increment index
end loop
heap created. Now sort it.
set sorted to last
loop (until all data sorted)
exchange (heap, 0, sorted)
decrement sorted
reheapDown (heap, 0, sorted)
end loop
End heapSort
Original list 23 78 45 8 32 56
7/31/2019 data structure notes for computer engineering note no:2
62/63
Unsorted
After heap78 32 56 8 23 45
HeapAfter pass 1and reheap
56 32 45 8 23 78
SortedHeapAfter pass 2and reheap
45 32 23 8 56 78
SortedHeapAfter pass 3and reheap
32 8 23 45 56 78
SortedHeap
After pass 4and reheap
23 8 32 45 56 78
SortedHeapAfter pass 5and reheap
8 23 32 45 56 78
SortedHeap
After pass 6and reheap
8 23 32 45 56 78
Sorted
7/31/2019 data structure notes for computer engineering note no:2
63/63
Heap Sort: Summary
Heap sort uses a heap data structure to
improve selection sort and make the running
time asymptotically optimal
Running time is O(n log n) like merge sort,
but unlike selection, insertion, or bubble sorts
Sorts in place like insertion, selection or
bubble sorts, but unlike merge sort