Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sorting 1 Devon M. Simmonds University of North Carolina, Wilmington TIME: Tuesday/Thursday 11:11:50am in 1012 & Thursday 3:30-5:10pm in 2006. Office hours:

Similar presentations


Presentation on theme: "Sorting 1 Devon M. Simmonds University of North Carolina, Wilmington TIME: Tuesday/Thursday 11:11:50am in 1012 & Thursday 3:30-5:10pm in 2006. Office hours:"— Presentation transcript:

1 Sorting 1 Devon M. Simmonds University of North Carolina, Wilmington TIME: Tuesday/Thursday 11:11:50am in 1012 & Thursday 3:30-5:10pm in 2006. Office hours: TR 1-2pm or by appointment. Office location: CI2046. Email: simmondsd[@]uncw.edu

2 Objectives To introduce basic sort algorithms: –Exchange/bubble sort –Insertion sort –Selection soft 2

3 3 What is sorting? Given n elements, arrange them in an increasing or decreasing order by some attribute. Simple algorithms: O(n 2 ) Insertion sort Selection sort Bubble sort Shell sort … Fancier algorithms: O(n log n) Heap sort Merge sort Quick sort … Specialized algorithms: O(n) Bucket sort Radix sort

4 4 Exchange (Bubble) Sort Algorithm (for a list a with n elements) –Repeat until list a is sorted for each i from n-1 downto 1 – if(a[i] < a[i-1]) »exchange (a[i], a[i-1]) –Result: After the k th pass, the first k elements are sorted.

5 Example 17 21 6 10 16 12 15 4 5

6 6 Try it out: Bubble sort Insert 31, 16, 54, 4, 2, 17, 6

7 7 Bubble Sort Code def bubbleSort(self, a): for i in range(len(a)): for j in range(len(a)-1, 0, -1): if(a[j] < a[j-1]): #exchange items temp = a[j-1] a[j-1] = a[j] a[j] = temp

8 8 Insertion Sort: Idea Algorithm (for a list a[0..n] with n elements) –for each i from 1 to n-1 put the i th element in the correct place among the first i+1 elements –Result: After the k th pass, the first k elements are sorted.

9 Example 17 21 6 10 16 12 15 4 9

10 10 Try it out: Insertion sort Insert 31, 16, 54, 4, 2, 17, 6

11 11 Insertion Sort Code def insertionSort(self, a): for i in range(1, len(a)): #insert a(i) in correct position in a(0).. a(i) temp = a[i] j = i-1 while (j >= 0 and a[j] > temp): if(a[j] > temp): a[j+1] = a[j] j -= 1 a[j+1] = temp

12 12 Selection Sort: idea Algorithm (for a list a with n elements) –for each i from 1 to n-1 Find the smallest element, put it in position i-1 –Result: After the k th pass, the first k elements are sorted.

13 Example 17 21 6 10 16 12 15 4 13

14 14 Try it out: Selection sort Insert 31, 16, 54, 4, 2, 17, 6

15 15 Selection Sort Code def selectionSort(self, a): for i in range(len(a)-1): #find smallest of items i, i+1, i+2,.., size()-1 and #exchange smallest with item in position i sIndex = i smallest = a[i] #j = i + 1 #while (j < len(a)): for j in range(i+1, len(a)): if(a[j] < smallest): sIndex = j smallest = a[j] #exchange items temp = a[i] a[i] = smallest a[sIndex] = temp

16 16 QuickSort (Partition Exchange) : idea Algorithm (for a list a with n elements) –.

17 QuickSort Example 17 21 6 10 16 12 15 4 17

18 18 Try it out: QuickSort Insert 31, 16, 54, 4, 2, 17, 6

19 19 QuickSort Code def quickSort(self, array, start, end): left = start right= end if (right - left < 1): return else:#at least 2 elements to be sorted pivot = array[start] while (right> left): while (array[left] <= pivot and left < right): left += 1 while (array[right] > pivot and right >= left): right -=1 if (right> left): swap(array, left, right) right -= 1 left += 1 #swap array[start] and array[right] temp = array[start] array[start] = array[right] array[right] = temp self.quickSort(array, start, right- 1); self.quickSort(array, right+ 1, end)

20 20 MergeSort: idea Algorithm (for a list a with n elements)

21 MergeSort Example 17 21 6 10 16 12 15 4 21

22 22 Try it out: MergeSort Insert 31, 16, 54, 4, 2, 17, 6

23 23 MergeSort Code def mergeSort(self, alist): if len(alist)>1: mid = len(alist)//2 lefthalf = alist[:mid] righthalf = alist[mid:] print("Splitting ",alist, "into", lefthalf, "and", righthalf) self.mergeSort(lefthalf) self.mergeSort(righthalf) self.merge(lefthalf, righthalf, alist) def merge(self, lefthalf, righthalf, alist): print("Merging ", lefthalf, "and", righthalf) i=0 j=0 k=0 while i<len(lefthalf) and j<len(righthalf): if lefthalf[i]<righthalf[j]: alist[k]=lefthalf[i] i=i+1 else: alist[k]=righthalf[j] j=j+1 k=k+1 while i<len(lefthalf): alist[k]=lefthalf[i] i=i+1 k=k+1 while j<len(righthalf): alist[k]=righthalf[j] j=j+1 k=k+1

24 Summary 24

25 25 ______________________ Devon M. Simmonds Computer Science Department University of North Carolina Wilmington _____________________________________________________________ Qu es ti ons? Reading from course text:

26 26 What is sorting? Given n elements, arrange them in an increasing or decreasing order by some attribute. Simple algorithms: O(n 2 ) Fancier algorithms: O(n log n) Comparison lower bound:  (n log n) Specialized algorithms: O(n) Handling huge data sets Insertion sort Selection sort Bubble sort Shell sort … Heap sort Merge sort Quick sort … Bucket sort Radix sort External sorting


Download ppt "Sorting 1 Devon M. Simmonds University of North Carolina, Wilmington TIME: Tuesday/Thursday 11:11:50am in 1012 & Thursday 3:30-5:10pm in 2006. Office hours:"

Similar presentations


Ads by Google