Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sorting Algorithms and Analysis Robert Duncan. Refresher on Big-O  O(2^N)Exponential  O(N^2)Quadratic  O(N log N)Linear/Log  O(N)Linear  O(log N)Log.

Similar presentations


Presentation on theme: "Sorting Algorithms and Analysis Robert Duncan. Refresher on Big-O  O(2^N)Exponential  O(N^2)Quadratic  O(N log N)Linear/Log  O(N)Linear  O(log N)Log."— Presentation transcript:

1 Sorting Algorithms and Analysis Robert Duncan

2 Refresher on Big-O  O(2^N)Exponential  O(N^2)Quadratic  O(N log N)Linear/Log  O(N)Linear  O(log N)Log  O(1)Constant Hierarchy of Big-O functions from slowest to fastest

3 Generic running times NO(log N)O(N)O(N log N)O(N^2)O(2^N) 101012 8382464256 1287 89616384 3.4x10^38 1024101024102401048576 1.8x10^308

4 O(N log N) vs. O(N^2)

5 Two Common Categories Sorting Algorithms of O(N^2)  Bubble Sort  Selection Sort  Insertion Sort Sorting Algorithms of O(N log N)  Heap Sort  Merge Sort  Quick Sort

6 For small values of N  It is important to note that all algorithms appear to run equally as fast for small values of N.  For values of N from the thousands to the millions, The differences between O(N^2) and O(N log N) become dramatically apparent

7 O(N^2) Sorts  Easy to program  Simple to understand  Very slow, especially for large values of N  Almost never used in professional software

8 Bubble Sort  The most inefficient of the O(n^2) algorithms  Simplest sorting algorithm available  Works by comparing sequential items, and swapping them if the first one is larger than the second. It makes as many passes through an array as are needed to complete the sort

9 Bubble Sort – Pass 1 2 3 6 4 1 5 2 3 6 4 1 5 2 3 4 6 1 5 2 3 4 1 6 5 2 3 4 1 5 6

10 Bubble Sort – Pass 2 2 3 4 1 5 6 2 3 4 1 5 6 2 3 1 4 5 6 2 3 1 4 5 6 2 3 1 4 5 6

11 Bubble Sort – Pass 3 2 3 1 4 5 6 2 1 3 4 5 6 2 1 3 4 5 6 2 1 3 4 5 6 2 1 3 4 5 6

12 Bubble Sort – Pass 4 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6

13 Selection Sort  More efficient than Bubble Sort, but not as efficient as Insertion Sort  Works by finding the largest element in the list and swapping it with the last element, effectively reducing the size of the list by 1.

14 Selection Sort – Pass 1-3 681024 6842 6248

15 Selection Sort – Pass 4-5 426810 2468 2468

16 Insertion Sort  One of the most efficient of the O(n^2) algorithms  Roughly twice as fast as bubble sort  Works by taking items from unsorted list and inserting them into the proper place.

17 Insertion Sort 22 35 223548

18 Insertion Sort 13223548 813223548 81322273548

19 O(N log N) Sorts  Fast  Efficient  Complicated, not easy to understand  Most make extensive use of recursion and complex data structures

20 Heap Sort  Slowest O(N log N) algorithm.  Although the slowest of the O(N log N) algorithms, it has less memory demands than Merge and Quick sort.

21 Heap Sort  Works by transferring items to a heap, which is basically a binary tree in which all parent nodes have greater values than their child nodes. The root of the tree, which is the largest item, is transferred to a new array and then the heap is reformed. The process is repeated until the sort is complete.

22 Forming the heap from an unsorted array 112614219327 214711 1926

23 Populating the new array 32 214711 1926

24 Reforming the heap 32 19 2711 1426

25 Reforming the heap 32 26 2711 1419

26 Repeat the process 3226 2711 1419

27 Repeat the process 3226 14 711 219

28 Repeat the process 3226 19 711 214

29 Repeat the process 322619 711 214

30 Merge Sort  Uses recursion. Slightly faster than heap, but uses twice as much memory from the 2 nd array.  Sometimes called “divide and conquer” sort.  Works by recursively splitting an array into two equal halves, sorting the items, then re- merging them back into a new array.

31 11261423218721 32187211126142 11261423218721

32 11262141832721

33 18327211126214 11262141832721

34 71821322111426

35 21114267182132 71821322111426

36 21114267182132

37 27111418212632

38 Quick Sort  The most efficient O(N log N) algorithm available  Works by first randomly choosing a pivot point which is hopefully a median element in the array. All elements less than the pivot are transferred to a new array, and all elements greater are transferred to a second array.

39 Quick Sort  These new arrays are recursively quick sorted until they have been split down to a single element. The sorted elements smaller than the pivot are placed in a new array, then the pivot is placed after, and finally the elements greater than the pivot. These elements are now sorted.

40 Quick Sort  Note: Although it is the quickest sorting algorithm, a badly chosen pivot may cause Quick Sort to run at O(N^2).  Different versions of quick sort address this problem in different ways.  For example, one way is to randomly choose 3 elements, then use the median element as the pivot.

41 112614232187215 Pivot

42 112614232187215 11275 Pivot

43 11275 Pivot 112614232187215

44 112614232187215 11275 25 Pivot

45 112614232187215 25711275 2525 Pivot

46 112614232187215 25711 275 2525

47 What’s the point

48 Binary Searching  Binary searches achieve O(log N) efficiency on sorted data  Similar to High-Low game  Each execution eliminates half of the elements to search for  Although hashing offers a quicker search of O(1), binary searches are simpler, and use much less memory.

49 Binary Searching 27111416232629323743 14?

50 Binary Searching 27111416232629323743 14?

51 Binary Searching 27111416232629323743 14

52 Conclusion…  O(N^2) algorithms are almost never used in professional software  Quick sort is generally considered to be the best overall sorting algorithm currently available.


Download ppt "Sorting Algorithms and Analysis Robert Duncan. Refresher on Big-O  O(2^N)Exponential  O(N^2)Quadratic  O(N log N)Linear/Log  O(N)Linear  O(log N)Log."

Similar presentations


Ads by Google