Presentation is loading. Please wait.

Presentation is loading. Please wait.

Selection Sort main( ) { int a[ ] = { 17, 6, 13,12, 2 } ; int i, j, t ; for ( i = 0 ; i <= 3 ; i++ ) { for ( j = 1 ; j <= 4 ; j++ ) { if ( a[ i ] > a[

Similar presentations


Presentation on theme: "Selection Sort main( ) { int a[ ] = { 17, 6, 13,12, 2 } ; int i, j, t ; for ( i = 0 ; i <= 3 ; i++ ) { for ( j = 1 ; j <= 4 ; j++ ) { if ( a[ i ] > a["— Presentation transcript:

1

2 Selection Sort main( ) { int a[ ] = { 17, 6, 13,12, 2 } ; int i, j, t ; for ( i = 0 ; i <= 3 ; i++ ) { for ( j = 1 ; j <= 4 ; j++ ) { if ( a[ i ] > a[ j ] ) { t = a [ i ] ; a[ i ] = a[ j ] ; a[ j ] = t ; } } } for ( i = 0 ; i <= 4 ; i++ ) printf ( ”%d”, a[ i ] ) ; } i + 17 6 13 12 2 i 6 17 13 12 2 0 - 1 0 - 2 0 - 3 0 - 4 6 17 13 12 2 2 17 13 12 6 2 1 - 2 2 2 2 6 2 6 12 13 17 12 6 12 17 13 6 1 - 3 6 17 13 12 1 - 4 13 17 12 12 17 13 2 - 3 2 - 4 13 17 3 - 4 j

3 Bubble Sort main( ) { int a[ ] = { 17, 6, 13, 12, 2 } ; int i, j, t ; for ( j = 0 ; j <= 3 ; j++ ) { for ( i = 0 ; i <= 3 ; i++ ) { if ( a[ i ] > a[ i + 1 ] ) { t =a[ i ] ; a[ i ] = a[ i + 1 ] ; a[ i + 1 ] = t ; } } } for ( i = 0 ; i <= 4 ; i++ ) printf ( ”%d”, a[ i ] ) ; } - j 17 6 13 12 2 i 6 17 13 12 2 0 - 1 1 - 2 2 - 3 3 - 4 6 13 17 12 2 6 13 12 17 2 6 13 12 2 17 6 13 12 20 - 1 6 12 13 2 6 12 2 13 6 12 2 6 2 12 2 6 1 - 2 2 - 3 13 17 0 - 1 1 - 2 12 13 17 0 - 1 i+1 17 13 17 17

4 void main() { int a=10,b=20; char x=1,y=0; if(a,b,x,y) { printf("EXAM"); } } What is the output? 1) XAM is printed 2) exam is printed 3) Compiler Error 4) Nothing is printed Answer: 4 Answer

5 Output of the following #include void main() { char letter =`Z`; printf("\n%c",letter); } 1) Z 2) 90 3) Error 4) Garbage Value ANSWER: 1) Z

6 void main() { int s=0; while(s++<10) { if(s<4 && s<9) continue; printf("\n%d\t",s); } } 1) 1 2 3 4 5 6 7 8 9 2) 1 2 3 10 3) 4 5 6 7 8 9 10 4) 4 5 6 7 8 9 ANSWER: 3

7 # include # define a 10 main() { printf("%d..",a); foo(); printf("%d",a); } void foo() { #define a 50 } 1) 10..10 2) 10..50 3) Error 4) 0 ANSWER: 1

8 printf(scanf(“%d”,&a); 1. a 2. &a 3. 1 4. 2

9 INSERTION SORT Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. On a repetition, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain.

10 An insertion sort partitions the array into two regions

11 Sorting problem: Given an array of N integers, rearrange them so that they are in increasing order. Insertion sort Move left-to-right through array. Exchange next element with larger elements to its left, one-by-one.

12 Sorting problem: Given an array of N integers, rearrange them so that they are in increasing order. Insertion sort Brute-force sorting solution. Move left-to-right through array. Exchange next element with larger elements to its left, one-by-one.

13 Sorting problem: Given an array of N integers, rearrange them so that they are in increasing order. Insertion sort Brute-force sorting solution. Move left-to-right through array. Exchange next element with larger elements to its left, one-by-one.

14 Sorting problem: Given an array of N integers, rearrange them so that they are in increasing order. Insertion sort Brute-force sorting solution. Move left-to-right through array. Exchange next element with larger elements to its left, one-by-one.

15 Sorting problem: Given an array of N integers, rearrange them so that they are in increasing order. Insertion sort Brute-force sorting solution. Move left-to-right through array. Exchange next element with larger elements to its left, one-by-one.

16 Sorting problem: Given an array of N integers, rearrange them so that they are in increasing order. Insertion sort Brute-force sorting solution. Move left-to-right through array. Exchange next element with larger elements to its left, one-by-one.

17 Sorting problem: Given an array of N integers, rearrange them so that they are in increasing order. Insertion sort Brute-force sorting solution. Move left-to-right through array. Exchange next element with larger elements to its left, one-by-one.

18 Sorting problem: Given an array of N integers, rearrange them so that they are in increasing order. Insertion sort Brute-force sorting solution. Move left-to-right through array. Exchange next element with larger elements to its left, one-by-one.

19 Sorting problem: Given an array of N integers, rearrange them so that they are in increasing order. Insertion sort Brute-force sorting solution. Move left-to-right through array. Exchange next element with larger elements to its left, one-by-one.

20 Sorting problem: Given an array of N integers, rearrange them so that they are in increasing order. Insertion sort Brute-force sorting solution. Move left-to-right through array. Exchange next element with larger elements to its left, one-by-one.

21 Sorting problem: Given an array of N integers, rearrange them so that they are in increasing order. Insertion sort Brute-force sorting solution. Move left-to-right through array. Exchange next element with larger elements to its left, one-by-one.

22 Sorting problem: Given an array of N integers, rearrange them so that they are in increasing order. Insertion sort Brute-force sorting solution. Move left-to-right through array. Exchange next element with larger elements to its left, one-by-one.

23 Sorting problem: Given an array of N integers, rearrange them so that they are in increasing order. Insertion sort Brute-force sorting solution. Move left-to-right through array. Exchange next element with larger elements to its left, one-by-one.

24 Sorting problem: Given an array of N integers, rearrange them so that they are in increasing order. Insertion sort Brute-force sorting solution. Move left-to-right through array. Exchange next element with larger elements to its left, one-by-one.

25 Sorting problem: Given an array of N integers, rearrange them so that they are in increasing order. Insertion sort Brute-force sorting solution. Move left-to-right through array. Exchange next element with larger elements to its left, one-by-one.

26 Sorting problem: Given an array of N integers, rearrange them so that they are in increasing order. Insertion sort Brute-force sorting solution. Move left-to-right through array. Exchange next element with larger elements to its left, one-by-one.

27 An insertion sort of an array of five integers

28 BUCKET SORT 29, 25, 3, 49, 9, 37, 21, 43 3 9 29 25 21 37 49 43

29 29 LINEAR/SEQUENTIAL SEARCH A sequential search of a list/array begins at the beginning of the list/array and continues until the item is found or the entire list/array has been searched. It can be applied on both sorted and unsorted array.

30 #include int main() { int a[10],i,n,m,c=0; printf("Enter the size of an array: "); scanf("%d",&n); printf("Enter the elements of the array: "); for(i=0;i<=n-1;i++) scanf("%d",&a[i]); printf("Enter the number to be search: "); scanf("%d",&m); for(i=0;i<=n-1;i++){ if(a[i]==m){ c=1; break; } } if(c==0) printf("The number is not in the list"); else printf("The number is found"); return 0;

31 82134657109111214130 64141325335143538472939597966 Binary Search lo Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo]  value  a[hi]. Ex. Binary search for 33. hi

32 Binary Search Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo]  value  a[hi]. Ex. Binary search for 33. 82134657109111214130 64141325335143538472939597966 lo hi mid Mid= (lo+hi)/2

33 Binary Search Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo]  value  a[hi]. Ex. Binary search for 33. 82134657109111214130 64141325335143538472939597966 lo hi

34 Binary Search Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo]  value  a[hi]. Ex. Binary search for 33. 82134657109111214130 64141325335143538472939597966 lo midhi

35 Binary Search Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo]  value  a[hi]. Ex. Binary search for 33. 82134657109111214130 64141325335143538472939597966 lohi

36 Binary Search Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo]  value  a[hi]. Ex. Binary search for 33. 82134657109111214130 64141325335143538472939597966 lohimid

37 Binary Search Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo]  value  a[hi]. Ex. Binary search for 33. 82134657109111214130 64141325335143538472939597966 lo hi

38 Binary Search Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo]  value  a[hi]. Ex. Binary search for 33. 82134657109111214130 64141325335143538472939597966 lo hi mid

39 Binary Search Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo]  value  a[hi]. Ex. Binary search for 33. 82134657109111214130 64141325335143538472939597966 lo hi mid

40 SPACE COMPLEXITY Space Complexity of an algorithm is total space taken by the algorithm with respect to the input size. Space complexity includes both Auxiliary space and space used by input. Auxiliary Space is the extra space or temporary space used by an algorithm

41 TIME complexity The time complexity of an algorithm: The amount of time taken by an algorithm to run as a function of the length of the string representing the input. The time complexity of an algorithm is commonly expressed using big O notation

42 Worst case complexity :denoted as T(n), which is defined as the maximum amount of time taken on any input of size n. T(n) = O(n) is called a linear time algorithm, and an algorithm with T(n) = O(2 n ) is said to be an exponential time algorithm. constant time : O(1) Average case complexity Best case complexity

43

44

45

46

47

48 public void insertionSort(Comparable[] arr) { for (int i = 1; i < arr.length; ++i) { Comparable temp = arr[i]; int pos = i; // Shuffle up all sorted items > arr[i] while (pos > 0 && arr[pos-1].compareTo(temp) > 0) { arr[pos] = arr[pos–1]; pos--; } // end while // Insert the current item arr[pos] = temp; } Insertion Sort Analysis outer loopouter times inner loop inner times

49 49 Bucket sort Assume N elements of A uniformly distributed over the range [0,1] Create M equal-sized buckets over [0,1], s.t., M≤N Add each element of A into appropriate bucket Sort each bucket internally Can use recursion here, or Can use something like InsertionSort Return concatentation of buckets Average case running time Θ (N) assuming each bucket will contain Θ (1) elements 49 Cpt S 223. School of EECS, WSU

50 Bucket-Sort Bucket-Sort: sorting numbers in the interval U = [0; 1). For sorting n numbers, 1. partition U into n non-overlapping intervals, called buckets, 2. put the input numbers into their buckets, 3. sort each bucket using a simple algorithm, e.g., Insertion-Sort, 4. concatenate the sorted lists What is the worst case running time of Bucket-Sort?

51 Bucket-Sort Example Let S be a list of n key-element items with keys in [0; N - 1]. Bucket-sort uses the keys as indices into auxiliary array B: I the elements of B are lists, so-called buckets I Phase 1: I empty S by moving each item (k; e) into its bucket B[k] I Phase 2: I for i = 0; : : : ;N -1 move the items of B[k] to the end of S Performance: I phase 1 takes O(n) time I phase 2 takes O(n + N) time Thus bucket-sort is O(n + N).

52

53

54

55 Sorting Procedures H Selection Sort H Bubble Sort H Shell Sort H Shuttle Sort H Heap Sort H Merge Sort H Radix Sort H Quick Sort  qsort( ) Recursion Fastest? log 2 n n2n2

56 main( ) { int a[ ] = {17, 6, 13, 12, 2 } ; Quick Sort At Work qsort ( a, 5, sizeof ( int ), fun ) ; for ( i = 0 ; i <= 4 ; i++ ) printf ( ”\n%d”, a[i] ) ; } fun ( int *i, int *j ) { return ( *i - *j ) ; } int i ; int fun ( int *, int * ) ; Comparison Function

57

58

59 Insertion Sort while some elements unsorted: Using linear search, find the location in the sorted portion where the 1 st element of the unsorted portion should be inserted Move all the elements after the insertion location up one position to make space for the new element 13214579472238743666942957816016 45 666045 the fourth iteration of this loop is shown here


Download ppt "Selection Sort main( ) { int a[ ] = { 17, 6, 13,12, 2 } ; int i, j, t ; for ( i = 0 ; i <= 3 ; i++ ) { for ( j = 1 ; j <= 4 ; j++ ) { if ( a[ i ] > a["

Similar presentations


Ads by Google