1 / 36

DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 6 : SORTING

NURUL HASLINDA NGAH BP-37 SEMESTER I 2010/2011. DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 6 : SORTING. OVERVIEW. At the end of this chapter you’ll be learnt about: Binary tree Selection sort Insertion sort Heap sort Bubble sort Quick sort External sorting Merge sort

ramya
Download Presentation

DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 6 : SORTING

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. NURUL HASLINDA NGAH BP-37 SEMESTER I 2010/2011 DATA STRUCTURE & ALGORITHMS(BCS 1223)CHAPTER 6 : SORTING

  2. OVERVIEW • At the end of this chapter you’ll be learnt about: • Binary tree • Selection sort • Insertion sort • Heap sort • Bubble sort • Quick sort • External sorting • Merge sort • Binary merge sort.

  3. Introduction • Sorting & searching are fundamental operations in computer science • Sorting – refers to the operation of arranging data in some given order, such as increasing or decreasing, with numerical, or alphabetically, with character data • Searching – refers to the operation of finding the location of a given item in a collection of items • Frequently applied to a file of records

  4. Introduction • Each record in a file F can contain many fields, but there may be one particular field whose values uniquely determine the records in the file • Such a field K is called a primary key, and the values k1,k2, … in such a field are called keys • Sorting the file F usually refers to sorting F with respect to a particular primary key, and searching in F refers to searching for the record with a given key value

  5. Sorting • Let A be a list of n elements A1, A2, …, An in memory • Sorting A refers to the operation of rearranging the contents of A so that they are increasing in order • A1 << A2 << A3 << … << An • Since A has n elements, there are n! ways that the contents can appear in A. These ways correspond precisely to the n! permutations of 1,2,…,n. Accordingly, each sorting algorithm must take care of these n! possibilities

  6. Sorting (example…) • Suppose an array DATA contains 8 elements as follows: • DATA: 77, 33, 44, 11, 88, 22, 66, 55 • After sorting, DATA must appear in memory as follows: • DATA: 11, 22, 33, 44, 55, 66, 77, 88 • Since DATA consists of 8 elements, there are 8! = 40 320 ways that the numbers 11, 22, …, 88 can appear in DATA

  7. Sorting: Bubble Sort • Let A be a list of n numbers. Sorting A refers to the operation of rearranging the elements of A so they are in increasing order, i.e, so that • A[1] < A[2] < A[3] < … < A[n] • For example, suppose A originally is the list • 8, 4, 19, 2, 7, 13, 5, 16 • After sorting, A is the list • 2, 4, 5, 7, 8, 13, 16, 19

  8. Sorting: Bubble Sort • Sorting may also mean arranging numerical data in decreasing order or arranging non-numerical data in alphabetical order • A is frequently a file of records and sorting A refers to rearranging the records of A so that the values of given key ordered

  9. Sorting: Bubble Sort • Suppose the list of numbers A[1], A[2], …, A[n] is in memory. The bubble sort algorithm works as follows: • Step 1: Compare A[1] and A[2] and arrange them in the desired order, so that A[1] < A[2]. Then compare A[2] and A[3] and arrange them so that A[2] < A[3]. Continue until we compare A[n-1] with A[n] and arrange them so that A[n-1] < A[n]. (During step 1, the largest element is “bubbled up” to the nth position or “sinks” to the nth position) • Step 2: Repeat step 1 with one less comparison; that is, now we stop after we compare and possibly rearrange A[n-2] and A[n-1]. (Step 2 involves n-2 comparisons and when step 2 is completed, the second largest element will occupy A[n-1] • Step 3: Repeat step 1 with two fewer comparisons; that is we stop after we compare and possibly rearrange A[n-3] and A[n-2] • Step n-1: Compare A[1] with A[2] and arrange them so that A[1] < A[2] • After n-1 step, the list will be sorted in increasing order. The process sequentially traversing thru all or part of a list is frequently called a “pass”, so each of the above steps is called a pass. Accordingly, the bubble sort algorithm requires n-1 passes, where n is the number of input items

  10. Sorting: Bubble Sort (example…) • Suppose the following numbers are stored in an array A: • 32, 51, 27, 85, 66, 23, 13, 57 • We apply the bubble sort to the array. We discuss each pass separately. • Pass 1: we have the following comparisons: • Compare A1 and A2. Since 32 < 51, the list is not altered • Compare A2 & A3. Since 51 > 27, interchange 51 and 27 as follows: • 32, 27, 51, 85, 66, 23, 13, 57 • Compare A3 & A4. Since 51 < 85, the list is not altered. • Compare A4 & A5. Since 85 > 66, interchange 85 and 86 as follows • 32, 27, 51, 66, 85, 23, 13, 57 • Compare A5 & A6. Since 85 > 23, interchange 85 and 23 as follows • 32, 27, 51, 66, 23, 85, 13, 57 • Compare A6 & A7. Since 85 > 13, interchange 85 and 13 as follows • 32, 27, 51, 66, 23, 13, 85, 57 • Compare A7 & A8. Since 85 > 57, interchange 85 and 57 as follows • 32, 27, 51, 66, 23, 13, 57, 85 • At the end of this first pass, the largest number, 85 has moved to the last position. However, the rest of the numbers are not sorted, even though some of them have changed their position.

  11. Sorting: Bubble Sort (example…) • Pass 2 • 27 , 32 , 51 , 66 , 23 , 13 , 57 , 85 • 27 , 32 , 51 , 23 , 66 , 13 , 57 , 85 • 27 , 32 , 51 , 23 , 13 , 66 , 57 , 85 • 27 , 32 , 51 , 23 , 13 , 57 , 66 , 85 • At the end of Pass 2, the 2nd largest number, 66, has moved its way down to the next-to-last position. • Pass 3: • 27 , 32 , 23 , 51 , 13 , 57 , 66 , 85 • 27 , 32 , 23 , 13 , 51 , 57 , 66 , 85 • Pass 4: • 27 , 23 , 32 , 13 , 51 , 57 , 66 , 85 • 27 , 23 , 13 , 32 , 51 , 57 , 66 , 85 • Pass 5: • 23 , 27 , 13 , 32 , 51 , 57 , 66 , 85 • 23 , 13 , 27 , 32 , 51 , 57 , 66 , 85 • Pass 6: • 13 , 23 , 27 , 32 , 51 , 57 , 66 , 85

  12. Sorting: Bubble Sort (example…) • Pass 7. Finally, A1 is compared with A2. Since 13 < 23, no interchange takes place. • Since the list has 8 elements; it is sorted after the 7th pass (in this example, the list was actually sorted after the 6th pass)

  13. Sorting: Bubble Sort Algorithm (Bubble Sort) BUBBLE(DATA, N) Here DATA is an array with N elements. This algorithm sorts the elements in DATA. 1. Repeat step 2 and 3 for K = 1 to N-1 2. Set PTR := 1. [Initializes pass pointer PTR] 3. Repeat while PTR <= N – K: [Execute pass.] 4. (a) if DATA[PTR] > DATA[PTR+1], then: Interchange DATA[PTR] and DATA[PTR+1] [End of If structure] (b) Set PTR := PTR + 1 [End of inner loop] [End of step 1 outer loop] 4. Exit

  14. Bubble Sort: Exercise • Using the bubble sort algorithm, find the number of comparisons and interchanges for the following letters in alphabetical order. P E O P L E

  15. Quicksort • Let A be a list of n data items. “Sorting A” refers to the operation of rearranging the elements of A so that they are in some logical order, such as numerically ordered when A contains numerical data, or alphabetically ordered when A contains character data • Quicksort is an algorithm of divide-and-conquer type. The problem of sorting a set is reduced to the problem of sorting 2 smaller sets.

  16. Quicksort: Example… • Suppose A is the following list of 12 numbers: • 44 , 33 , 11 , 55 , 77 , 90 , 40 , 60 , 99 , 22 , 88 , 66 • The reduction steps of the quicksort algorithm finds the final position of one of the numbers; in this example, we use the 1st number, 44. Beginning with the last number, 66, scan the list from right to the left, comparing each number with 44 and stopping at the first number less than 44. the number is 22. Interchange 44 and 22 to obtain the list • 22 , 33 , 11 , 55 , 77 , 90 , 40 , 60 , 99 , 44 , 88 , 66

  17. Quicksort: Example… • Beginning with 22, next scan the list in the opposite direction, from left to right, comparing each number with 44 and stopping at the first number greater than 44. The number is 55. Interchange 44 and 55 to obtain the list. • 22 , 33 , 11 , 44 , 77 , 90 , 40 , 60 , 99 , 55 , 88 , 66 • (observe that the numbers 22, 33 and 11 to the left of 44 are each less than 44). Beginning this time with 55, now scan the list in the original direction, from right to left, until meeting the first number less than 44. It is 40. Interchange 44 and 40 to obtain the list • 22 , 33 , 11 , 40 , 77 , 90 , 44 , 60 , 99 , 55 , 88 , 66

  18. Quicksort: Example… • (Again, the numbers to the right of 44 are each greater than 44). Beginning with 40, scan the list from left to right. The first number greater that 44 is 77. Interchange 44 and 77 to obtain the list • 22 , 33 , 11 , 40 , 44 , 90 , 77 , 60 , 99 , 55 , 88 , 66 • (Again, the numbers to the left of 44 are each less than 44). Beginning with 77, scan the list from right to left seeking a number less than 44. We do not meet such number before meeting 44. This means all numbers have been scanned & compared with 44. Furthermore, all numbers less than 44 now form the sublist of numbers to the left of 44, & all numbers greater than 44 now form the sublist of numbers to the right of 44 as shown below • 22 , 33 , 11 , 40 , 44 , 90 , 77 , 60 , 99 , 55 , 88 , 66 First sublist Second sublist

  19. Quicksort: Example… • Thus, 44 is correctly placed in its final position, and the task of sorting the original list A has now been reduced to the task of sorting each of the above sublist. • The above reduction step is repeated with each sublist containing 2 or more elements. Since we can process only one sublist at a time, we must be able to keep track of some sublists for future processing. This is accomplished by using 2 stacks, called LOWER and UPPER, to temporarily hold such sublists. • The addresses of the first & last elements of each sublist, called its boundary values, are pushed onto the stacks LOWER and UPPER respectively and the reduction step is applied to a sublist only after its boundary values are removed from the stacks.

  20. Quicksort: Example… • Consider the above list. The algorithm begins by pushing the boundary values 1 and 12 of A onto the stacks to yield • LOWER: 1 UPPER: 12 • In order to apply the reduction step, the algorithm first removes the top values 1 and 12 from the stacks, leaving • LOWER: (empty) UPPER: (empty) • And then applies the reduction step to the corresponding list A[1], A[2], …, A[12]. The reduction step, as executed above, finally places the first element, 44, in A[5]. Accordingly, the algorithm pushes the boundary values 1 and 4 of the first sublist & the boundary values 6 and 12 of the second sublist onto the stacks to yield • LOWER: 1, 6 UPPER: 4, 12 • In order to apply the reduction step again, the algorithm removes the top values, 6 and 12 from the stacks leaving • LOWER: 1 UPPER: 4

  21. Quicksort: Example… • Then, applies the reduction step to the corresponding sublist A[6], A[7], …, A[12]. The reduction step changes this list as in the following figure. Observe that the second sublist has only one element. Accordingly, the algorithm pushes only the boundary values 6 and 10 of the first sublist onto the stacks to yield • LOWER: 1, 6 UPPER: 4, 10 • And so on. The algorithm ends when the stacks do not contain any sublist to be processed by the reduction step Second sublist First sublist 15/3

  22. Quicksort: Exercise… • Suppose S is the following list of 14 alphabetic characters. Suppose the characters in S are to be sorted alphabetically. Use the quicksort algorithm to find the final position of the first character D. • D A T A S T R U C T U R E

  23. Insertion Sorting • Suppose an array A with n elements A[1], A[2], ……, A[n] is in memory. The insertion sort algorithms scans A from A[1] to A[n], inserting each element A[K] into its proper position in the previously sorted sub array A[1], A[2], …, A[K-1]. That is: • Pass 1: A[1] by itself is trivially sorted • Pass 2: A[2] is inserted either before or after A[1] so that: A[1], A[2] is sorted • Pass 3: A[3] is inserted into its proper place in A[1], A[2], that is, before A[1], between A[1] and A[2], or after A[2], so that: A[1], A[2], A[3] is sorted • Pass 4: A[4] is inserted into its proper place in A[1], A[2], A[3] so that: A[1], A[2], A[3], A[4] is sorted • Pass N: A[N] is inserted into its proper place in A[1], A[2], …., A[N-1] so that: A[1], A[2], …. , A[N] is sorted

  24. Insertion Sorting : Example… • Suppose an array A contains 8 elements as follows: • 77 , 33 , 44 , 11 , 88 , 22 , 66 , 55 • Figure 2 illustrates the insertion sort algorithm. The circle element indicates the A[K] in each pass of the algorithm an the arrow indicates the proper place for inserting A[K]

  25. Insertion Sorting : Example… Figure 3

  26. Insertion Sorting : Algorithm… (Insertion sort) INSERTION (A, N) This algorithm sorts the array A with N elements 1. Set A[0] := -∞. [Initializes sentinel element.] 2. Repeat steps 3 to 5 for K = 2, 3, …, N: 3. Set TEMP := A[K] and PTR := K-1 4. Repeat while TEMP < A[PTR]: (a) Set A[PTR+1]:=A[PTR]. [Moves element forward] (b) Set PTR := PTR – 1 [End of loop] 5. Set A[PTR+1] := TEMP. [Inserts element in proper place.] [End of step 2 loop] 6. Return Observe that there is an inner loop which is essentially controlled by the variable PTR, and there is an outer loop which uses K as an index.

  27. Insertion Sorting : Exercise… • Suppose an array A contains 10 elements as follows: • 90 , 30 , 40 , 20 , 70 , 10 , 50 , 5 , 60 , 80 • Sort the element using an insertion sorting. Show how the process take place.

  28. Selection Sorting • Suppose an array A with n elements A[1], A[2], …, A[N] is in memory. The selection sort algorithm for sorting A works as follows. • 1st, find the smallest element in the list and put it in the first position. Then, find the 2nd smallest element in the list and put it in the second position and so on. More precisely: • Pass 1: Find the location LOC of the smallest in the list of N elements A[1], A[2], ….., A[N[ and then interchange A[LOC] and A[1]. Then: A[1] is sorted • Pass 2: Find the location LOC of the smallest in the sublist of N-1 elements A[2], A[3], ….., A[N[ and then interchange A[LOC] and A[2]. Then: A[1], A[2] is sorted, since A[2] <= A[3] • Pass N-1: Find the location LOC of the smaller of the elements A[N-1], A[N] and then interchange A[LOC] and A[N-1]. Then: A[1], A[2], …., A[N] is sorted, since A[N-1] <= A[N]

  29. Selection Sorting: Example… • Suppose an array A contains 8 elements as follows: • 77 , 33 , 44 , 11 , 88 , 22 , 66 , 55 • Applying the selection sort algorithm to A yields the data in figure 4. Observe that LOC gives the location of the smallest among A[K], A[K+1], …, A[N] during pass K. • The circle elements indicate the elements which are to be interchanged

  30. Selection Sorting: Example…

  31. Selection Sorting: Exercise… • Suppose an array A contains 8 elements as follows: • 90 , 30 , 40 , 20 , 70 , 10 , 50 , 5 , 60 , 80 • Sort the elements using selection sorting. Shows step-by-step (in tabular form)

  32. Merging • Suppose A is sorted list with r elements and B is a sorted list with s elements. The operation that combines the elements of A and B into a single sorted list C with n = r + s elements is called merging • One simple way to merge is to place the elements of B after the elements of A & then use some sorting algorithm on the entire list. This method does not take advantage of the fact that A & B are individually sorted • Suppose we have 2 lines of students sorted by increasing heights and suppose we want to merge them into a single sorted line. The new line is formed by choosing, at each step, the shorter of the 2 students who are at the head of their respective lines. When one of the lines has no more students, the remaining students line up at the end of the combined line

  33. Merging (cont…) • We may translated into formal algorithm which merges a sorted r-element array A and sorted s-element array B into a sorted array C with n = r + s • We must always keep track of the locations of the smallest element of A and the smallest element of B which have not yet been placed in C • Let NA and NB denote these locations, respectively. Also, let PTR denote the location in C to be filled. Thus, initially, we set NA := 1, NB := 1 and PTR:=1. At each step of the algorithm, we compare • A[NA] and B[NB] • And assign the smaller element to C[PTR]. Then, we increment PTR by setting PTR:= PTR + 1, and either we increment NA by setting NA:=NA+1 or increment NB by setting NB:= NB+1, according to whether the new element in C has come from A or from B. • If NA > r, then the remaining elements of B are assigned to C; or if NB > s, then the remaining elements of A are assigned to C

  34. Merge Sort: example… • Suppose an array A with n elements A[1], A[2], …, A[N] is in memory. The merge sort algorithm which sorts A will first be described by means of a specific example • Suppose the array A contains 14 elements as follows: • 66 , 33 , 40 , 22 , 55 , 88 , 60 , 11 , 80 , 20 , 50 , 44 , 77 , 30 • Each pass of the merge sort algorithm will start at the beginning of the array A and merge pairs of sorted sub arrays as follows: • Pass 1: Merge each pair of elements to obtain the following list of sorted pairs: • 33 , 66 22 , 40 55 , 88 11 , 60 20 , 80 44 , 50 30 , 77 • Pass 2: Merge each pair of pairs to obtain the following list of sorted quadruplets: • 22, 33 , 40, 66 11, 55 , 60, 88 20 , 44, 50, 80 30, 77

  35. Merge Sort: example… • Pass 3: Merge each pair of sorted quadruplets to obtain the following two sorted sub arrays: • 11 , 22 , 33 , 40 , 55 , 60 , 66 , 88 20 , 30 , 44 , 50 , 77 , 80 • Pass 4: Merge the two sorted sub arrays to obtain the single sorted array • 11 , 20 , 22 , 30 , 33 , 40 , 40 , 50 , 55 , 60 , 66 , 77 , 80 , 88 • The original array A is now sorted.

  36. End of Chapter

More Related