Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.

Similar presentations


Presentation on theme: "Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These."— Presentation transcript:

1 Fall 2013 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu Sharif University of Technology 1 Fundamentals of Programming Session 17 These slides have been created using Deitel’s slides

2 Outlines Sorting Arrays Bubble sort Selection sort Insertion sort Merge Sort Quick Sort 2

3 Sorting data (i.e., placing the data into a particular order such as ascending or descending) is one of the most important computing applications. Sorting data Important computing application Virtually every organization must sort some data Massive amounts must be sorted Sorting: an operation that segregates items into groups according to specified criterion. A = { 3 1 6 2 1 3 4 5 9 0 } A = { 0 1 1 2 3 3 4 5 6 9 } 3 Sorting Arrays

4 There are many different types of sorting algorithms, but the primary ones are: Bubble Sort Selection Sort Insertion Sort Merge Sort Quick Sort Shell Sort Heap Sort Radix Sort Swap Sort... 4 Sorting Arrays …

5 Bubble sort (sinking sort) Several passes through the array Successive pairs of elements are compared If increasing order (or identical), no change If decreasing order, elements exchanged Repeat these steps for every element 5 Bubble sort

6 Example: Go left to right, and exchange elements as necessary One pass for each element Original: 3 4 2 7 6 Pass 1: 3 2 4 6 7 (elements exchanged) Pass 2: 2 3 4 6 7 Pass 3: 2 3 4 6 7 (no changes needed) Pass 4: 2 3 4 6 7 Small elements "bubble" to the top (like 2 in this example) 6 Bubble sort …

7 Swapping variables int x = 3, y = 4; y = x; x = y; What happened? Both x and y are 3! Need a temporary variable Solution int x = 3, y = 4, temp = 0; temp = x; // temp gets 3 x = y; // x gets 4 y = temp; // y gets 3 Figure 6.15 sorts the values in the elements of the 10- element array a into ascending order. 7 Bubble sort …

8 8

9 9

10 10 Bubble sort …

11 11 Bubble sort …

12 12 Bubble sort …

13 Bubble sort is one of the simplest sorting algorithms to understand and implement. Due to its simplicity, bubble sort is often used to introduce the concept of an algorithm, or a sorting algorithm, to introductory computer science students. Bubble sort is a stable sort. Its complexity is Best case: O(n) Average case: O(n 2 ) Worst case: O(n 2 ) 13 Bubble sort …

14 Selection sort This type of sorting is called "Selection Sort" because it works by repeatedly element. It works as follows: First find the smallest in the array and exchange it with the element in the first position, then find the second smallest element and exchange it with the element in the second position, and continue in this way until the entire array is sorted. 14 Selection sort

15 15

16 Pseudocode of selection sort SELECTION_SORT (A) 1. For i ← 0 to n-2 do 2. minj ← i; 3. minx ← A[i] 4. For j ← i + 1 to n do 5. If A[j] < minx then 6. minj ← j 7. minx ← A[j] 8. A[minj] ← A [i] 9. A[i] ← minx 16 Selection sort …

17 17 Selection sort …

18 Generally it acts better that bubble sort. Selection sort is not a stable sort. Its complexity is Best case: O(n 2 ) Average case: O(n 2 ) Worst case: O(n 2 ) 18 Selection sort …

19 Insertion sort algorithm somewhat resembles selection sort. Array is imaginary divided into two parts: sorted one and unsorted one. At the beginning, sorted part contains first element of the array and unsorted one contains the rest. At every step, algorithm takes first element in the unsorted part and inserts it to the right place of the sorted one. When unsorted part becomes empty, algorithm stops. 19 Insertion sort

20 20 Insertion sort …

21 Pseudocode of selection sort INSERTION_SORT (A) 1. FOR j ← 1 TO length[A] 2. DO key ← A[j] 3. {Put A[j] into the sorted sequence A[1.. j − 1]} 4. i ← j − 1 5. WHILE i > 0 and A[i] > key 6. DO A[i +1] ← A[i] 7. i ← i − 1 8. A[i + 1] ← key 21 Insertion sort …

22 22 Insertion sort …

23 23 Insertion sort …

24 Insertion sort is similar to the selection sort. It typically makes fewer comparisons than selection sort. Insertion sort is a stable sort. Its complexity is Best case: O(n) Average case: O(n 2 ) Worst case: O(n 2 ) 24 Insertion sort …

25 A merge sort works as follows: Divide the unsorted list into n sub-lists, each containing 1 element (a list of 1 element is considered sorted). Repeatedly merge sub-lists to produce new sorted sub-lists until there is only 1 sub-list remaining. This will be the sorted list. 25 Merge sort

26 26

27 MERGE-SORT (A, p, r) 1. IF p < r // Check for base case 2. THEN q = FLOOR[(p + r)/2] 3. MERGE_SORT (A, p, q) // Divide step 4. MERGE_SORT (A, q + 1, r) // Divide step 5. MERGE (A, p, q, r) // Conquer step. 27 Merge sort …

28 28

29 29

30 MERGE (A, p, q, r ) 1. n 1 ← q − p + 1 2. n 2 ← r − q 3. Create arrays L[1.. n 1 + 1] and R[1.. n 2 + 1] 4. FOR i ← 1 TO n 1 5. DO L[i] ← A[p + i − 1] 6. FOR j ← 1 TO n 2 7. DO R[j] ← A[q + j ] 8. L[n 1 + 1] ← ∞ 9. R[n 2 + 1] ← ∞ 10. i ← 1 11. j ← 1 12. FOR k ← p TO r 13. DO IF L[i ] ≤ R[ j] 14. THEN A[k] ← L[i] 15. i ← i + 1 16. ELSE A[k] ← R[j] 17. j ← j + 1 30 Merge sort …

31 31 Merge sort …

32 Generally, merge sort acts better than three sorting algorithms introduced previously. Merge sort parallelizes well due to use of the divide and conquer method. Merger sort is a stable sort as long as the merge operation is implemented properly. Its complexity is Best case: O(nlogn) Average case: O(nlogn) Worst case: O(nlogn) 32 Merge sort …

33 Quicksort, also known as partition-exchange sort, uses these steps. 1. Choose any element of the array to be the pivot. 2. Divide all other elements (except the pivot) into two partitions. 1. All elements less than the pivot must be in the first partition. 2. All elements greater than the pivot must be in the second partition. 3. Use recursion to sort both partitions. 4. Join the first sorted partition, the pivot, and the second sorted partition. 33 Quick sort

34 34 Quick sort … Initial Step - First Partition Sort Left Partition in the same way

35 function quicksort(array) less, equal, greater := three empty arrays if length(array) > 1 pivot := select any element of array for each x in array if x < pivot then add x to less if x = pivot then add x to equal if x > pivot then add x to greater quicksort(less) quicksort(greater) array := concatenate(less, equal, greater) 35 Quick sort …

36 36 Quick sort …

37 The best pivot creates partitions of equal length (or lengths differing by 1). The worst pivot creates an empty partition (for example, if the pivot is the first or last element of a sorted array). Typically Quick sort is not a stable sort, but there are some stable variations. Its complexity is Best case: O(nlogn) Average case: O(nlogn) Worst case: O(n 2 ) 37 Quick sort …

38 Try to write a C code for quick sort. 38 Exercise


Download ppt "Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These."

Similar presentations


Ads by Google