Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tigbur 16.1 Complexity Sorting. Complexity סימון אסימפטוטי.

Similar presentations


Presentation on theme: "Tigbur 16.1 Complexity Sorting. Complexity סימון אסימפטוטי."— Presentation transcript:

1 Tigbur 16.1 Complexity Sorting

2 Complexity

3

4 סימון אסימפטוטי

5 נוסחאות שימושיות לחישוב סכומים

6

7 תוכנית לדוגמא - 1

8 תוכנית לדוגמא - 2

9 תוכנית לדוגמא - 3

10 תוכנית לדוגמא - 4

11 Recurrence Relation: Insertion Sort, Selection Sort, Bubble Sort T(n) = T(n - 1) + O(n) T(1) = O(1)[constant time] Array of size 1 – solved in constant time (base case) These relationships are called a recurrence relation because the function T( ) occurs on both sides of the = sign; it should remind you of recursive specifications This recurrence relation describes the sorting functions: insertion, selection, bubble If we could solve the recurrence relation we would know the complexity of these sort algorithms since T(n) is the time for them to execute

12 Recurrence Relations to Know and Love T(n) = T(n – 1) + O(n) Selection / Insertion / Bubble Sort O(n 2 ) T(n) = T(n – 1) + O(1) Sequential Search O(n) T(n) = T(n/2) + O(1) Binary Search O(log 2 n)

13 Sorting

14 Selection Sort (a.k.a.Max-Sort) Pseudo Code: For i=0,1, …,n-2 Run over the array from i onwards. Find j- the index of the minimal item. Swap the values in indexes i,j.

15 Running Time of selection sort Note first that the running time is always pretty much the same. Finding the minimal value always requires us to go over the entire sub-array. There is no shortcut, and we never stop early. First, we have an array of size n-1 to scan, then size n-2, then size n-2 … We ’ ve already analyzed this pattern: Running time is O(n 2 ).

16 16

17 17

18 18

19 19

20 Insert Sort public static void insertSort(int[] data){ for(int i=1; i<data.length; i++){ //assume before index i the array is sorted insert(data,i); //insert i into array 0...(i-1) } private static void insert(int[] data, int index) { int value = data[index]; int j; for(j=index-1; j>=0 && data[j]>value; j-- ){ data[j+1]=data[j]; } data[j+1] = value; }

21 Complexity of Insertion Sort Worst case complexity: O(n 2 ) – If the original array is sorted backwards we insert all the way in.First into an array of size 1, then 2, then 3 … Is this pattern familiar? Best case complexity: Ω(n) – The array is already sorted. Each insert stops after 1 step, but we call insert Ω(n) times.

22

23 23 Ternary Search static int Ternary(int []data, int key) { int pivot1, pivot2, lower=0, upper=data.length-1; do { pivot1 = (upper-lower)/3+lower; pivot2 = 2*(upper-lower)/3+lower; if (data[pivot1] > key){ upper = pivot1-1; } else if (data[pivot2] > key){ upper = pivot2-1; lower = pivot1+1; } else { lower = pivot2+1; } } while((data[pivot1] != key) && (data[pivot2] != key) && (lower <= upper)); if (data[pivot1] == key) return pivot1; if (data[pivot2] == key) return pivot2; else return -1; }

24 24 Complexity Just like binary search, but instead of dividing to two, we divide to three. Same analysis as in binary search can show that the complexity is O(nlog 3 n). We note that: log a n = log b n/log b a Therefore O(nlog 3 n)=O(nlogn)

25 25 Find Sum x+y=z static boolean FindSum(int []data, int z){ for (int i=0; i < data.length; i++){ for (int j=i+1; j<data.length; j++){ if (data[i]+data[j] == z){ System.out.println("x=" + data[i] + " and y=" + data[j]); return true; } return false; } How do we show the complexity?

26 Complexity Remember the analysis for Bubble Sort: First loop has n iterates. Inner loop:

27 27 Find Sum in sorted array x+y=z static boolean FindSumSorted(int []data, int z){ int lower=0, upper=data.length-1; while ((data[lower]+data[upper] != z) && (lower < upper)) { if (data[lower]+data[upper] > z) upper--; else if (data[lower]+data[upper] < z) lower++; } if (lower >= upper) { return false; } else { System.out.println("x=" + data[lower] + " and y=" + data[upper]); } return true; } Complexity?

28 Shell sort 28

29 29

30 static public void shellSort(int[ ] data) { int newest, current, increment, newItem, size; size = data.length; increment = size/2; while (increment > 0) { for (newest = increment; newest < size; newest++) { current = newest; newItem = data[newest]; while ((current >= increment) && (data[current - increment] > newItem)) { data[current] = data[current - increment]; current -= increment; } data[current] = newItem; }//close for loop increment /= 2; }//close while loop { 30

31 31 Complexity The complexity is just like regular insertion sort – O(n^2) Answers of O(n^2*log(n)) were accepted Shell sort make less moves because it moves the values closer to their correct locations in the early stages, using large jumps The last sorting loop is usually fast as the array is almost sorted


Download ppt "Tigbur 16.1 Complexity Sorting. Complexity סימון אסימפטוטי."

Similar presentations


Ads by Google