590 likes | 690 Views
SORTING. main( ) {. Selection Sort. int a[ ] = { 17, 6, 13,12, 2 } ;. int i, j, t ;. j. i. 17 6 13 12 2. 0 - 1. for ( i = 0 ; i <= 3 ; i++ ). 6 17 13 12 2. {. 6 17 13 12 2. 0 - 2. for ( j = 1 ; j <= 4 ; j++ ). i +. 0 - 3. 6 17 13 12 2. {.
E N D
main( ) { Selection Sort int a[ ] = { 17, 6, 13,12, 2 } ; int i, j, t ; j i 17 6 13 12 2 0 - 1 for ( i = 0 ; i <= 3 ; i++ ) 6 17 13 12 2 { 61713122 0 - 2 for ( j = 1 ; j <= 4 ; j++ ) i+ 0 - 3 61713122 { 0 - 4 21713126 if ( a[ i ] > a[ j ] ) 1 - 2 2 1317126 { 2 1 - 3 1217136 t = a [ i ] ; a[ i ] = a[ j ] ; 2 1 - 4 6171312 a[ j ] = t ; } 2 6 2 - 3 131712 } 121713 2 6 } 2 - 4 for ( i = 0 ; i <= 4 ; i++ ) 2 6 12 3 - 4 13 17 printf ( ”%d”, a[ i ] ) ; }
main( ) { Bubble Sort int a[ ] = { 17, 6, 13, 12, 2 } ; i i+1 17 6 13 12 2 int i, j, t ; 0 - 1 6 17 13 12 2 for ( j = 0 ; j <= 3 ; j++ ) 6 1317 12 2 1 - 2 { 2 - 3 6 13 1217 2 - j for ( i = 0 ; i <= 3 ; i++ ) 3 - 4 6 13 12 217 { if ( a[ i ] > a[ i + 1 ] ) 17 6 13 12 2 0 - 1 { 6 12 13 2 17 1 - 2 a[ i ] = a[ i + 1 ] ; t =a[ i ] ; 2 - 3 6 12 213 17 a[ i + 1 ] = t ; 6 12 2 0 - 1 } 13 17 } 1 - 2 13 17 6 2 12 } for ( i = 0 ; i <= 4 ; i++ ) 0 - 1 12 13 17 2 6 printf ( ”%d”, a[ i ] ) ; }
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
Output of the following#include<stdio.h> void main() { char letter =`Z`; printf("\n%c",letter); } • 1) Z 2) 90 3) Error 4) Garbage Value • ANSWER: 1) Z
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
# include<stdio.h> # 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
printf(scanf(“%d”,&a); • a • &a • 1 • 2
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
BUCKET SORT 29 , 25 , 3 , 49 , 9 , 37 , 21 , 43 29 49 3 25 37 9 21 43
LINEAR/SEQUENTIAL SEARCH • A sequential searchof 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.
#include<stdio.h> 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;
Binary Search Binary search. Given value and sorted array a[], find index isuch that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo]value a[hi]. Ex. Binary search for 33. 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lo hi
Binary Search Binary search. Given value and sorted array a[], find index isuch that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo]value a[hi]. Ex. Binary search for 33. Mid= (lo+hi)/2 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lo mid hi
Binary Search Binary search. Given value and sorted array a[], find index isuch that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo]value a[hi]. Ex. Binary search for 33. 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lo hi
Binary Search Binary search. Given value and sorted array a[], find index isuch that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo]value a[hi]. Ex. Binary search for 33. 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lo mid hi
Binary Search Binary search. Given value and sorted array a[], find index isuch that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo]value a[hi]. Ex. Binary search for 33. 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lo hi
Binary Search Binary search. Given value and sorted array a[], find index isuch that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo]value a[hi]. Ex. Binary search for 33. 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lo mid hi
Binary Search Binary search. Given value and sorted array a[], find index isuch that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo]value a[hi]. Ex. Binary search for 33. 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lohi
Binary Search Binary search. Given value and sorted array a[], find index isuch that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo]value a[hi]. Ex. Binary search for 33. 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lohimid
Binary Search Binary search. Given value and sorted array a[], find index isuch that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo]value a[hi]. Ex. Binary search for 33. 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lohimid
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
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
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(2n) is said to be an exponential time algorithm.constant time : O(1) • Average case complexity • Best case complexity
outer loop outer times inner loop inner times Insertion Sort Analysis publicvoid 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; } }
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
Bucket-Sort • Bucket-Sort: sorting numbers in the interval U = [0; 1). • For sorting n numbers, • partition U into n non-overlapping intervals, called buckets, • put the input numbers into their buckets, • sort each bucket using a simple algorithm, e.g., Insertion-Sort, • concatenate the sorted lists • What is the worst case running time of Bucket-Sort?