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 i j
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 i 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 Answer
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
void main() { int s=0; while(s++<10) { if(s<4 && s<9) continue; printf("\n%d\t",s); } } 1) ) ) ) ANSWER: 3
# include # define a 10 main() { printf("%d..",a); foo(); printf("%d",a); } void foo() { #define a 50 } 1) ) ) Error 4) 0 ANSWER: 1
printf(scanf(“%d”,&a); 1. a 2. &a
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.
An insertion sort partitions the array into two regions
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.
An insertion sort of an array of five integers
BUCKET SORT 29, 25, 3, 49, 9, 37, 21,
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.
#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;
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
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 lo hi mid Mid= (lo+hi)/2
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 lo hi
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 lo midhi
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 lohi
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 lohimid
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 lo hi
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 lo hi mid
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 lo hi mid
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(2 n ) is said to be an exponential time algorithm. constant time : O(1) Average case complexity Best case complexity
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 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, 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?
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).
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
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
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 the fourth iteration of this loop is shown here