Presentation is loading. Please wait.

Presentation is loading. Please wait.

SEQUENCES SORTING. Sorting zInput: (unsorted) sequence of n elements a 1, a 2, …, a n zOutput: the same sequence in increasing order (or in nondecreasing.

Similar presentations


Presentation on theme: "SEQUENCES SORTING. Sorting zInput: (unsorted) sequence of n elements a 1, a 2, …, a n zOutput: the same sequence in increasing order (or in nondecreasing."— Presentation transcript:

1 SEQUENCES SORTING

2 Sorting zInput: (unsorted) sequence of n elements a 1, a 2, …, a n zOutput: the same sequence in increasing order (or in nondecreasing order if there are equal elements)

3 Sorting zWhat do we measure ? - Comparisons and swaps whichever dominates - However, comparisons dominate most of the time

4 Sorting: A Different View zsorting can be seen as a permutation problem: ydetermine a permutation of indices i 1, i 2, …, i n such that a[i 1 ]<=a[i 2 ]<=…<=a[i n ] yn! possible permutations

5 A Few Assumptions zwe “extend” dictionary ADT to include a sort method ysort the keys of the key-element pairs (k, e) yimplemented by an array-based sequence, unless specifically stated

6 DICTIONARY METHODS zsize() zisEmpty() zfindElement(k) zfindAllElements(k) zreturn no. of items in D ztest if D is empty zreturn element of item with key equal to k, else NO_SUCH_KEY zreturn an enumeration of all elements with key k

7 Sorting zFocus on the process zMethodology might be useful in generating solutions to other problems (i.e. brainstorming)

8 Sorting Algorithms zO(n 2 ): Bubble, Selection, Insertion Sort zO(n log n): Merge, Quick, Heap Sort* zO(n)[input is qualified]: Bucket Sort

9 Bubble Sort zview the sequence to be in a vertical instead of a horizontal position (easier to visualize) zthe items are like bubbles in a water tank with weights according to their keys zthen, each pass results in the bubble rising to its proper level of weight

10 Bubble Sort - Pseudocode Algorithm BubbleSort for i  0 to n-1 for j  0 to n-i if (s[j].key > s[j+1].key) then swap (s[j],s[j+1])

11 Insertion Sort zRepeatedly insert element in its proper place Algorithm InsertionSort for i  1 to n-1 do x  S[i] insert x in the proper place in S[0]…S[i]

12 Insertion Sort Example

13 Insertion Sort TC zarray implementation: at most O(n) data moves and O(n) comparisons per pass [O(log n) if binary search is used] zlinked-list implementation: O(1) data moves but still O(n) comparisons per pass (binary search is not possible) zinsertion sort is on the average n*(n-1)/4 or O(n 2 )

14 Simple Sorting Algorithms zBubble Sort zSelection Sort zInsertion Sort zAll have running times of O(n 2 )

15 Advanced Sorting Algorithms

16 Merge - Query zGiven two sorted arrays A and B, arrange the elements to a third array C (in sorted order) zA[23,47,81,95] zB[7,14,39,55,62,74]

17 Merge - Operations

18 Merged List zA[23,47,81,95] zB[7,14,39,55,62,74] zC[7,14,23,39,47,55,62,74,81,95]

19 Merge Sort - General zDivide into two parts zSort left side, Sort right side zMerge left and right side

20 Merge Sort - Mathematical zDivide y divide S into 2 equal sequences S 1 and S 2 zRecurse y recursively sort sequences S 1 and S 2 zConquer ymerge sorted sequences S 1 and S 2 back into S

21 Merge Sort Example

22 Closer Look At Merging

23 Merge Sort TC t(n) = time to merge sort n elements t(n/2) = time to merge sort n/2 elements t(n) = 2t(n/2) + O(n), a recurrence relation ==> merge sort is O(n log n) {look at diagram} Note: merge sort requires extra space

24 Divide-and-Conquer zDivide yif input size < threshold, solve directly yelse, divide data into 2 or more subsets zRecurse yrecursively solve subproblems associated with subsets zConquer ytake solutions to subproblems ycombine into solution to original problem

25 Advanced Sorting zO(n log n) vs O(n 2 ) : significant zFor 10,000 records, n 2 = 100,000,000 n log n = 40,000 zif sorting using merge sort takes 40 seconds, insertion sort will take 28 hrs zThere’s another sorting algorithm (same level) which does not require additional space

26 Partitioning - Query zConsider this problem, Make an algorithm, that will arrange List L so that all items less than 50 will be to the left of 50 while all items greater than 50 will be at the right of 50 in one pass. List L [85,24,63,45,17,31,96,50].

27 Partitioning zCreate pointer at both ends of the list zIf a[0] 50 then it is in the right position else (wrong position) stop and wait for the right side to find a wrong entry If both wrong entries, swap z

28 Partitioning Example zpartitioning around pivot 50

29 Quick Sort zDivide: if S has at least 2 elements, select x from S called the pivot. Put elements of S into 3 subsequences: yL, all elements in S less than x yE, all elements in S equal to x yG, all elements in S greater than x zRecurse: recursively sort L and G zConquer: Put back elements into S in order by concatenating L, E, and G

30 Quick Sort Example znew pivot is selected each line

31 Quick Sort zPivot can be selected at random zUsually, it’s the rightmost or the leftmost entry zOn the average, it will be in the middle portion of the list

32 Quick Sort TC ztime complexity depends on pivot choice zaverage O(n log n) zworst O(n 2 ) - data is skewed, reverse order

33 O(n log n) Algorithms

34 Bucket Sort zWhat if you had 10,000 unsorted records and each record had a value between 1 to 10 which you had to arrange in increasing order (1,1,1,1,2,2,2,etc.)? zCan we beat O(n log n) ?

35 Bucket Sort zinput restriction: sequence S of n items with keys in the range [0,N-1] zcan sort S in O(n+N) time zif N is O(n), then sorting time is O(n) znote: NOT based on comparison

36 Pseudocode Algorithm BucketSort Input: Sequence S with integer keys in range [0,N-1] Output: S sorted let B=array of N initially empty sequences for each item x in S do k=key of x remove x from S and insert at end of sequence B[k] for i  0 to N-1 do for each item x in sequence B[i] do remove x from B[i] and insert at end of S

37 Other Sorting Algorithms zShell Sort Improvement of Insertion Sort -Sorts widely spaced elements (i.e. every 4th) -Diminishing gaps (364,121,40,1) - Insertion sort the last pass - Running time (estimated) between O(n 7/6 ) to O(n 3/2 ){more realistic)

38 Sorting - Summary zLooked at various sorting algorithms zAnalyzed their running time zComparisons and swaps/copies zImplementation described in the book zReview recursion*


Download ppt "SEQUENCES SORTING. Sorting zInput: (unsorted) sequence of n elements a 1, a 2, …, a n zOutput: the same sequence in increasing order (or in nondecreasing."

Similar presentations


Ads by Google