Download presentation
Presentation is loading. Please wait.
Published byĒΜιχαήλ Κωνσταντίνου Modified over 6 years ago
1
Subject Name : Data Structure Using C Unit Title : Sorting Methods
PREVIOUS HOME
2
Sorting Methods Recap Sorting Internal Sorting External Sorting
Class summary PREVIOUS NEXT
3
Recap Searching Basics Searching Techniques Linear Search
Linear Search (Algorithm) Linear Search (Analysis) Binary Search Binary Search (Algorithm) Binary Search (Analysis) PREVIOUS HOME NEXT
4
Sorting Methods Sorting
In computer science and mathematics, a sorting algorithm is an algorithm that puts elements of a list in a certain order. The most used orders are numerical order and lexicographical order. Efficient sorting is important to optimizing the use of other algorithms (such as search and merge algorithms) that require sorted lists to work correctly; it is also often useful for canonicalizing data and for producing human-readable output. Sorting Internal Sorting External Sorting PREVIOUS HOME NEXT
5
Sorting Methods Internal Sorting
Insertion sort Bubble sort Quick sort 2-Way Merge sort Heap sort Sorting Methods Internal Sorting An internal sort is any data sorting process that takes place entirely within the main memory of a computer. This is possible whenever the data to be sorted is small enough to all be held in the main memory. For sorting larger datasets, it may be necessary to hold only a chunk of data in memory at a time, since it wont all fit. The rest of the data is normally held on some larger, but slower medium, like a hard-disk. There are number of different types of Internal Sorting PREVIOUS HOME NEXT
6
Sorting Methods Internal Sorting Selection Sort Bubble Sort
Insertion Sort Quick Sort Heap Sort Shell Sorting Radix Sorting Other Sorting Algo. PREVIOUS HOME NEXT
7
Sorting Methods Selection Sort (1/2)
Selection sort is a sorting algorithm, specifically an in-place comparison sort. It has O(n2) complexity, making it inefficient on large lists, and generally performs worse than the similar insertion sort. Selection sort is noted for its simplicity, and also has performance advantages over more complicated algorithms in certain situations. The algorithm works as follows: Find the minimum value in the list Swap it with the value in the first position Repeat the steps above for remainder of the list (starting at the second position) PREVIOUS HOME NEXT
8
Sorting Methods Selection Sort (2/2) Iteration 1 Iteration 2 64 25 12
11 11 11 11 11 25 64 64 64 64 64 25 22 12 12 12 25 25 25 25 64 64 64 22 22 22 22 22 22 22 25 25 11 11 11 11 12 12 12 12 22 Iteration 3 Iteration 4 Iteration 5 11 11 11 11 11 11 12 12 12 12 12 12 64 25 22 22 22 22 25 64 64 64 25 25 22 22 25 25 64 64 PREVIOUS HOME NEXT
9
Sorting Methods Bubble Sort (1/2)
Bubble sort is a simple sorting algorithm. It works by repeatedly stepping through the list to be sorted, comparing two items at a time and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. Bubble sort has worst-case complexity О(n²), where n is the number of items being sorted. There exist many other sorting algorithms with substantially better worst-case complexity O(n log n), meaning that bubble sort should not be used when n is large. PREVIOUS HOME NEXT
10
Sorting Methods Bubble Sort (2/2) Iteration 1 Iteration 2 64 25 25 25
12 12 12 25 64 12 12 12 12 25 22 22 12 12 64 22 22 22 22 25 11 22 22 22 64 11 11 11 11 25 11 11 11 11 64 64 64 64 64 Iteration 3 Iteration 4 Iteration 5 12 12 12 12 11 11 22 22 11 11 12 12 11 11 22 22 22 22 25 25 25 25 25 25 64 64 64 64 64 64 PREVIOUS HOME NEXT
11
Sorting Methods Insertion Sort (1/2)
Insertion sort is a simple sorting algorithm, a comparison sort in which the sorted array (or list) is built one entry at a time. It is much less efficient on large lists than more advanced algorithms such as quick sort, heap sort or merge sort, but it has various advantages: Efficient on (quite) small data sets Efficient on data sets which are already substantially sorted: it runs in O(n + d) time, where d is the number of inversions More efficient in practice than most other simple O(n2) algorithms such as selection sort or bubble sort: the average time is n2/4 and it is linear in the best case Stable (does not change the relative order of elements with equal keys) In-place (only requires a constant amount O(1) of extra memory space) It is an online algorithm, in that it can sort a list as it receives it. PREVIOUS HOME NEXT
12
Sorting Methods Insertion Sort (2/2) 64 25 12 12 11 25 64 25 22 12 12
Iteration 1 Iteration 2 Iteration 3 Iteration 4 Iteration 5 PREVIOUS HOME NEXT
13
Sorting Methods Quick Sort (1/6)
Quick sort doesn't look at all like any of the sorts we have examined up until now. It is a partition sort. It is one of the speediest of these 'in place' array sorts. It is also one of the most complex to code. We say that it is faster than its siblings, but in fact, it is also an O(n2) algorithm. It gets its reputation for speed from the fact that it is O(n2). only in the worst case, which almost never occurs. In practice quick sort is an O(n Log n) algorithm. PREVIOUS HOME NEXT
14
Sorting Methods Quick Sort (2/6)
40 20 10 80 60 50 7 30 100 There are a number of ways to pick the pivot element. In this example, we will use the first element in the array Given a pivot, partition the elements of the array such that the resulting array consists of: One sub-array that contains elements >= pivot Another sub-array that contains elements < pivot The sub-arrays are stored in the original data array. Partitioning loops through, swapping elements below/above pivot. PREVIOUS HOME NEXT
15
Sorting Methods Quick Sort (3/6) 40 20 10 80 60 50 7 30 100
[0] [1] [2] [3] [4] [5] [6] [7] [8] (1) pivot_index = 0 too_big_index too_small_index While data[too_big_index] <= data[pivot] ++too_big_index While data[too_small_index] > data[pivot] --too_small_index If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] While too_small_index > too_big_index, go to 1. 40 20 10 80 60 50 7 30 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] (2) 40 20 10 80 60 50 7 30 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] (3) 40 20 10 80 60 50 7 30 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] (4) PREVIOUS HOME NEXT
16
Sorting Methods Quick Sort (4/6) 40 20 10 30 60 50 7 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8] Interchange Values (5) pivot_index = 0 too_big_index too_small_index 40 20 10 30 60 50 7 80 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] (6) While data[too_big_index] <= data[pivot] ++too_big_index While data[too_small_index] > data[pivot] --too_small_index If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] While too_small_index > too_big_index, go to 1. 40 20 10 30 60 50 7 80 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] (7) 40 20 10 30 7 50 60 80 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] Interchange Values (8) PREVIOUS HOME NEXT
17
Sorting Methods Quick Sort (5/6) 40 20 10 30 7 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8] (9) pivot_index = 0 too_big_index too_small_index 40 20 10 30 7 50 60 80 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] (10) While data[too_big_index] <= data[pivot] ++too_big_index While data[too_small_index] > data[pivot] --too_small_index If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] While too_small_index > too_big_index, go to 1. 40 20 10 30 7 50 60 80 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] (11) Interchange Values 7 20 10 80 40 50 60 30 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] (12) PREVIOUS HOME NEXT
18
Sorting Methods Quick Sort (6/6) pivot_index = 4 7 20 10 80 40 50 60
30 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] (13) too_big_index too_small_index While data[too_big_index] <= data[pivot] ++too_big_index While data[too_small_index] > data[pivot] --too_small_index If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index] While too_small_index > too_big_index, go to 1. 7 20 10 80 40 50 60 30 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] <= data[pivot] > data[pivot] Partition Result Now continue the same process over two sub lists. Sub list 1 Sub list 2 (14) PREVIOUS HOME NEXT
19
Sorting Methods Heap Sort (1/8)
Heapsort (method) is a comparison-based sorting algorithm, and is part of the selection sort family. Although somewhat slower in practice on most machines than a good implementation of quicksort, it has the advantage of a worst-case Θ(n log n) runtime. Heapsort is an in-place algorithm, but is not a stable sort. Plan of attack First, we will learn how to turn a binary tree into a heap Next, we will learn how to turn a binary tree back into a heap after it has been changed in a certain way Finally we will see how to use these ideas to sort an array PREVIOUS HOME NEXT
20
Sorting Methods 12 8 3 12 8 12 8 14 Heap Sort (2/8)
The Heap Property: A node has the heap property if the value in the node is as large as or larger than the values in its children 12 8 3 Blue node has heap property 12 8 Blue node has heap property 12 8 14 Blue node does not have heap property PREVIOUS HOME NEXT
21
Sorting Methods Heap Sort (3/8)
ShiftUp: Given a node that does not have the heap property, you can give it the heap property by exchanging its value with the value of the larger child. This is sometimes called sifting up. 12 14 8 12 Blue node has heap property 8 14 Blue node does not have heap property PREVIOUS HOME NEXT
22
Sorting Methods Heap Sort (4/8) Heap Construction Rules (1/2):
A tree consisting of a single node is automatically a heap We construct a heap by adding nodes one at a time: Add the node just to the right of the rightmost node in the deepest level If the deepest level is full, start a new level Examples: Add a new node here Add a new node here PREVIOUS HOME NEXT
23
Sorting Methods Heap Sort (5/8) Heap Construction Rules (2/2):
Each time we add a node, we may destroy the heap property of its parent node To fix this, we sift up But each time we sift up, the value of the topmost node in the sift may increase, and this may destroy the heap property of its parent node We repeat the sifting up process, moving up in the tree, until either We reach nodes whose values don’t need to be swapped (because the parent is still larger than both children), or We reach the root PREVIOUS HOME NEXT
24
Sorting Methods Heap Sort (6/8) Heap Construction Example (1/2) : 8, 10, 5, 12, 14 8 1 10 8 5 3 8 10 10 8 2 10 8 5 12 10 12 5 8 12 10 5 8 4 PREVIOUS HOME NEXT
25
Sorting Methods 12 10 5 8 14 12 14 5 8 10 14 12 8 10 5 Heap Sort (7/8)
Heap Construction Example (2/2) : 8, 10, 5, 12, 14 12 10 5 8 14 12 14 5 8 10 14 12 8 10 5 Complete Heap 5 Notice that heapified does not mean sorted PREVIOUS HOME NEXT
26
Sorting Methods 14 12 8 10 5 12 10 8 5 10 8 5 8 5 5 Heap Sort (8/8)
Sorting and Reheapifying (1/1): 14 12 8 10 5 (1) 12 10 8 5 (2) 10 8 5 (3) Removed Removed Removed 8 5 (4) 5 (5) 14 12 10 8 5 Removed Removed Sorted Array PREVIOUS HOME NEXT
27
Sorting Methods Shell Sort (1/9) :
Founded by Donald Shell and named the sorting algorithm after himself in Shell sort is a sorting algorithm that is a generalization of insertion sort, with two observations: insertion sort is efficient if the input is "almost sorted", and insertion sort is typically inefficient because it moves values just one position at a time. Best Case: The best case in the shell sort is when the array is already sorted in the right order. The number of comparisons is less. Worst Case: The running time of Shell sort depends on the choice of increment sequence. The problem with Shell’s increments is that pairs of increments are not necessarily relatively prime and smaller increments can have little effect. PREVIOUS HOME NEXT
28
Sorting Methods P H A S E I
Shell Sort Example (2/9) : The sorting increment will be calculated as floor(N/2) 18 32 12 2 38 33 16 5 Here N=8 , So increment= Floor(8/2) = 4 P H A S E I 18 and 38 stays at its current position because they are in order 18 32 12 5 38 33 16 2 32 and 33 stays at its current position because they are in order 18 32 12 5 38 33 16 2 12 and 16 stays at its current position because they are in order 18 32 12 5 38 33 16 2 18 32 12 5 38 33 16 2 2 and 5 need to be switched to be in order Change is occurred so continue with increment 4 again 18 32 12 2 38 33 16 5 PREVIOUS HOME NEXT
29
Sorting Methods P H A S E I Shell Sort Example (3/9) :
Increment Remains = 4 18 32 12 2 38 33 16 5 18 and 38 stays at its current position because they are in order P H A S E I 18 32 12 2 38 33 16 5 32 and 33 stays at its current position because they are in order 18 32 12 2 38 33 16 5 12 and 16 stays at its current position because they are in order 18 32 12 2 38 33 16 5 2 and 5 stays at its current position because they are in order 18 32 12 2 38 33 16 5 No Change is occurred Now Phase I Completed PREVIOUS HOME NEXT
30
Sorting Methods P H A S E II Shell Sort Example (4/9) :
Now increment= Floor(4/2) = 2 12 and 18 stays at its current position because they are in order 12 2 18 32 38 33 16 5 P H A S E II 2 and 32 stays at its current position because they are in order 12 2 18 32 38 33 16 5 18 and 38 stays at its current position because they are in order 12 2 18 32 38 33 16 5 32 and 33 stays at its current position because they are in order 12 2 18 32 38 33 16 5 12 2 18 32 38 33 16 5 38 and 16 need to be switched to be in order 12 2 18 32 16 33 38 5 33 and 5 need to be switched to be in order Change is occurred so continue with increment 2 again 12 2 18 32 16 5 38 33 PREVIOUS HOME NEXT
31
Sorting Methods P H A S E II Shell Sort Example (5/9) :
Increment Remains = 2 12 and 18 stays at its current position because they are in order 12 2 18 32 16 5 38 33 P H A S E II 2 and 32 stays at its current position because they are in order 12 2 18 32 16 5 38 33 12 2 18 32 16 5 38 33 18 and 16 need to be switched to be in order 12 2 16 32 18 5 38 33 32 and 5 need to be switched to be in order 18 and 38 stays at its current position because they are in order 12 2 16 5 18 32 38 33 12 2 16 5 18 32 38 33 32 and 33 stays at its current position because they are in order 12 2 16 5 18 32 38 33 Change is occurred so continue with increment 2 again PREVIOUS HOME NEXT
32
Sorting Methods P H A S E II Shell Sort Example (6/9) :
Increment Remains = 4 12 and 16 stays at its current position because they are in order 12 2 16 5 18 32 38 33 P H A S E II 2 and 5 stays at its current position because they are in order 12 2 16 5 18 32 38 33 12 2 16 5 18 32 38 33 16 and 18 stays at its current position because they are in order 12 2 16 5 18 32 38 33 5 and 32 stays at its current position because they are in order 18 and 38 stays at its current position because they are in order 12 2 16 5 18 32 38 33 12 2 16 5 18 32 38 33 32 and 33 stays at its current position because they are in order No Change is occurred Now Phase II Completed PREVIOUS HOME NEXT
33
Sorting Methods P H A S E III Shell Sort Example (7/9) :
Now increment= Floor(2/2) = 1 12 and 2 need to be switched to be in order 12 2 16 5 18 32 38 33 P H A S E III 12 and 16 stays at its current position because they are in order 2 12 16 5 18 32 38 33 2 12 16 5 18 32 38 33 16 and 5 need to be switched to be in order 16 and 18 stays at its current position because they are in order 2 12 5 16 18 32 38 33 18 and 32 stays at its current position because they are in order 2 12 5 16 18 32 38 33 32 and 33 stays at its current position because they are in order 2 12 5 16 18 32 38 33 2 12 5 16 18 32 38 33 38 and 33 need to be switched to be in order Change is occurred so continue with increment 1 again 2 12 5 16 18 32 33 38 PREVIOUS HOME NEXT
34
Sorting Methods P H A S E III Shell Sort Example (8/9) :
Increment Remains = 1 2 12 5 16 18 32 33 38 2 and 12 stays at its current position because they are in order P H A S E III 2 12 5 16 18 32 33 38 12 and 5 need to be switched to be in order 12 and 16 stays at its current position because they are in order 2 5 12 16 18 32 33 38 2 5 12 16 18 32 33 38 16 and 18 stays at its current position because they are in order 2 5 12 16 18 32 33 38 18 and 32 stays at its current position because they are in order 2 5 12 16 18 32 33 38 32 and 33 stays at its current position because they are in order 33 and 38 stays at its current position because they are in order 2 5 12 16 18 32 33 38 Change is occurred so continue with increment 1 again 2 5 12 16 18 32 33 38 PREVIOUS HOME NEXT
35
Sorting Methods P H A S E III Shell Sort Example (9/9) :
Increment Remains = 1 2 5 12 16 18 32 33 38 2 and 5 stays at its current position because they are in order P H A S E III 2 5 12 16 18 32 33 38 5 and 12 stays at its current position because they are in order 12 and 16 stays at its current position because they are in order 2 5 12 16 18 32 33 38 2 5 12 16 18 32 33 38 16 and 18 stays at its current position because they are in order 2 5 12 16 18 32 33 38 18 and 32 stays at its current position because they are in order 2 5 12 16 18 32 33 38 32 and 33 stays at its current position because they are in order 2 5 12 16 18 32 33 38 33 and 38 stays at its current position because they are in order No Change is occurred 2 5 12 16 18 32 33 38 PREVIOUS HOME NEXT Now Phase III Completed And Sorting Completed
36
Sorting Methods Radix Sort (1/4) :
In computer science, radix sort is a sorting algorithm that sorts integers by processing individual digits. Because integers can represent strings of characters (e.g., names or dates) and specially formatted floating point numbers, radix sort is not limited to integers. Radix sort is not a comparison sort, therefore it is not subject to the O(n log n) sorting lower bound Each element is represented by b bits (i.e. 32 bit integers) The algorithm performs a number of iterations; each iteration considers only r bits of each element at a time, with the ith pass sorting according to the ith group of the least significant bits PREVIOUS HOME NEXT
37
Sorting Methods Radix Sort Example (2/4) :
20 30 32 18 06 09 38 49 78 89 67 - Number of pass will be the number of digits of the maximum number. - Here maximum number is 89 and number of digits is 2. So, number of PASS=2 - The distribution will be from LSD (Less Significant Digit) PREVIOUS HOME NEXT
38
Sorting Methods Radix Sort Example (3/4) : PASS = 1 Finally the list
20 30 1 2 32 3 Distribution According to LSD 4 5 6 06 7 67 PREVIOUS HOME NEXT 8 18 38 78 9 09 49 89 Finally the list 20 30 32 06 67 18 38 78 09 49 89
39
Sorting Methods Radix Sort Example (4/4) : PASS = 2 Finally the list
06 09 1 18 2 20 3 30 32 38 Distribution According to MSD 4 49 5 6 67 Sorting Completed 7 78 8 89 PREVIOUS HOME NEXT 9 Finally the list 06 09 18 20 30 32 38 49 67 78 89
40
Sorting Methods External Sorting
External sorting is a term for a class of sorting algorithms that can handle massive amounts of data. External sorting is required when the data being sorted does not fit into the main memory of a computing device (usually RAM) and a slower kind of memory (usually a hard drive) needs to be used. One example of external sorting is the external merge sort algorithm PREVIOUS HOME NEXT
41
Sorting Methods External Sorting
For example, for sorting 900 megabytes of data using only 100 Read 100 MB of the data in main memory and sort by some conventional method. Write the sorted data to disk. Repeat steps 1 and 2 until all of the data is sorted in 100 MB chunks, which now need to be merged into one single output file. Read the first 10 MB of each sorted chunk (call them input buffers) in main memory (90 MB total) and allocate the remaining 10 MB for output buffer. Perform a 9-way merging and store the result in the output buffer. If the output buffer is full, write it to the final sorted file. If any of the 9 input buffers gets empty, fill it with the next 10 MB of its associated 100 MB sorted chunk or otherwise mark it as exhausted if there is no more data in the sorted chunk and do not use it for merging. PREVIOUS HOME NEXT
42
Sorting Methods Merge Sort (1/5) :
In computer science, merge sort or mergesort is an O(n log n) comparison-based sorting algorithm. In most implementations it is stable, meaning that it preserves the input order of equal elements in the sorted output. It is an example of the divide and conquer algorithmic paradigm. It was invented by John von Neumann in 1945. Conceptually, a merge sort works as follows: If the list is of length 0 or 1, then it is already sorted. Otherwise: Divide the unsorted list into two sub lists of about half the size. Sort each sub list recursively by re-applying merge sort. Merge the two sub lists back into one sorted list. PREVIOUS HOME NEXT
43
Sorting Methods Merge Sort (2/5) : Split the sequence at m=(i+j)/2
i=1, j=7, m=4 6 5 8 3 2 7 1 Make a recursive call to merge sort m := (i+j)/2= 2.5=2 6 5 8 3 2 7 1 Now sorting this part i=1, j=2, m=1 and make a recursive call 6 5 8 3 2 7 1 6 5 8 3 2 7 1 Sequence of length 1 is already sorted, so return Sequence of length 1 is sorted 6 5 8 3 2 7 1 Return control and merge these 2 sub-sequences into correct order 6 5 8 3 2 7 1 PREVIOUS HOME NEXT
44
Sorting Methods Merge Sort (3/5) :
Now sort the second part of this subsequence 5 6 8 3 2 7 1 Sequence of length 1 is sorted 5 6 8 3 2 7 1 Sequence of length 1 is sorted 5 6 8 3 2 7 1 Merge these two into correct order 5 6 8 3 2 7 1 Return control to previous call. Now merge these two sub-sequences into correct order 5 6 3 8 2 7 1 3 5 6 8 2 7 1 First part of sequence is now fully sorted PREVIOUS HOME NEXT
45
Sorting Methods Merge Sort (4/5) :
Control now returns to the very first call of merge sort A recursive call is now made on the second part of the sequence i=5, j=7, m=6 , Split sequence at item 6 5 6 8 3 2 7 1 Make a recursive call to sort these two items Split sequence at item 5 5 6 8 3 2 7 1 Make another recursive call on the first part of this subsequence 5 6 8 3 2 7 1 Sequence of length 1 is sorted 5 6 8 3 2 7 1 Make a recursive call on second part of sequence Sequence of length 1 is sorted 5 6 8 3 2 7 1 PREVIOUS HOME NEXT
46
Sorting Methods Merge Sort (5/5) :
Now merge these 2 sub-sequences into correct order 5 6 8 3 2 7 1 5 6 8 3 2 7 1 Make a recursive call with the remaining element 5 6 8 3 2 7 1 Sequence of length 1 is sorted 5 6 8 3 2 7 1 Now merge these 2 sub-sequences into correct order 5 6 8 3 1 2 7 Return to the first call Now merge these 2 sub-sequences into correct order 1 2 3 5 6 7 8 Merge sort is completed PREVIOUS HOME NEXT
47
Class summary Sorting Internal Sorting External Sorting PREVIOUS HOME
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.