0% found this document useful (0 votes)
67 views8 pages

Arrays 4

The document discusses various sorting algorithms like binary search, selection sort, and insertion sort. It provides code examples and explanations for each algorithm. For binary search, it discusses the number of comparisons required. For selection sort, it outlines the basic principle and provides pseudocode. For insertion sort, it describes the process of inserting elements and provides two implementations of insertion sort with output examples.

Uploaded by

ChiraG ArorA
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views8 pages

Arrays 4

The document discusses various sorting algorithms like binary search, selection sort, and insertion sort. It provides code examples and explanations for each algorithm. For binary search, it discusses the number of comparisons required. For selection sort, it outlines the basic principle and provides pseudocode. For insertion sort, it describes the process of inserting elements and provides two implementations of insertion sort with output examples.

Uploaded by

ChiraG ArorA
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Arrays:4

P. P. Chakrabarti

17-02-03

P.P.Chakrabarti, IIT Kharagpur

Binary Search:Recap
int bsearch(A, i,j, data) int *A; int i,j, data; { int k; if (i > j) return -1; /* BASIS CONDITION */ k = (i+j)/2; if (A[k] == data) return k; /* ANOTHER BASIS */ if (A[k] > data) return(bsearch(A, i, k-1,data)); else return(bsearch(A, k+1,j, data)); }
Search mid-point And then either stop Or move left/right Depending on Comparison result

17-02-03

P.P.Chakrabarti, IIT Kharagpur

Number of Comparisons made


Binary Search: Recurrence for No. of Comparisons T(n) = 1 if n ==1 = T(n/2) if n > 1 Solves to T(n) = 1 if n ==1 = ceiling(log2 n), if n > 1 /* ceiling (n) is the smallest integer ? n */ You may examine Linear Search in a similar way

17-02-03

P.P.Chakrabarti, IIT Kharagpur

Selection Sort Revisited:


Basic principle: Sel_Sort(S) { Let S be the set of be sorted. Let p = max element (S) S = S {p} Result = < p, Sel_Sort(S) > } main() { int A[100], i, size; scanf("%d",&size); for (i=0;i<size;i++) scanf("%d",&A[i]); selsort(A,size); for (i=0;i<size;i++) printf("%d, ",A[i]); printf("\n"); }

int selsort (int x[], int size) { int k, m, temp; for (k=0; k<size-1; k++) { m = max_loc(x, k, size); temp = x[k]; x[k] = x[m]; x[m] = temp; } } int max_loc(int x[], int k, int size) { int j, pos; pos = k; for (j=k+1; j<size; j++) if (x[j] > x[pos]) pos = j; return pos; }
P.P.Chakrabarti, IIT Kharagpur

17-02-03

Insertion Sort
Ins_Sort(A,B) /* A unsorted, B sorted */ { select a from A; A A {a}; insert a in B to get B keeping B sorted; Ins_Sort(A,B) } /* Initial & basis conditions??? */ insSort (int list[], int size) { int i, item; for (i=1; i<size; i++) { item = list[i] ; /* list[0] to list[i-1] is B and list [i] to list[size-1] is A */ /* INSERT item in its proper place between list[0] and list[i-1] by comparing upwards and pushing smaller elements down the array */ } insSort (int list[], int size) { int i, j, item; for (i=1; i<size; i++) { /* Insert the element in list[i] */ item = list[i] ; for (j=i-1; j>=0; j--) if (item > list[j]) { /* push elements down */ list[j+1] = list[j]; list[j] = item ; } else break; /*inserted, exit loop */ } }
P.P.Chakrabarti, IIT Kharagpur

17-02-03

Insertion Sort: Run


Another way : Ins_Sort(S) { Let S = sorted array of k elements; Read and insert a new element a in S to get a sorted array of k+1 elements; } /* Try to write code for this */ main() { int A[100], i, size; scanf("%d",&size); for (i=0;i<size;i++) scanf("%d",&A[i]); insSort(A,size); for (i=0;i<size;i++) printf("%d, ",A[i]); printf("\n"); }

insSort (int list[], int size) { int i,j, item; for (i=1; i<size; i++) { item = list[i] ; for (j=i-1; j>=0; j--) if (item > list[j]) { list[j+1] = list[j]; list[j] = item ; } else break; }} [ppchak]$ ./a.out 5 9 -1 3 8 5 9, 8, 5, 3, -1,
P.P.Chakrabarti, IIT Kharagpur

17-02-03

insSort (int list[], int size) { int i,j, item; for (i=1; i<size; i++) { printf("i = %d:: ",i); for (j=0;j<size;j++) printf("%d, ",list[j]); printf("\n"); item = list[i] ; for (j=i-1; j>=0; j--) if (item > list[j]) { list[j+1] = list[j]; list[j] = item ; } else break; }} main() { int A[100], i, size; scanf("%d",&size); for (i=0;i<size;i++) scanf("%d",&A[i]); insSort(A,size); printf("Result = "); for (i=0;i<size;i++) printf("%d, ",A[i]); printf("\n"); }

Look at the sorting!


[ppchak]$ ./a.out 8 2 9 4 7 6 2 1 5 i = 1:: 2, 9, 4, 7, 6, 2, 1, 5, i = 2:: 9, 2, 4, 7, 6, 2, 1, 5, i = 3:: 9, 4, 2, 7, 6, 2, 1, 5, i = 4:: 9, 7, 4, 2, 6, 2, 1, 5, i = 5:: 9, 7, 6, 4, 2, 2, 1, 5, i = 6:: 9, 7, 6, 4, 2, 2, 1, 5, i = 7:: 9, 7, 6, 4, 2, 2, 1, 5, Result = 9, 7, 6, 5, 4, 2, 2, 1,
P.P.Chakrabarti, IIT Kharagpur

17-02-03

Recap on Pointers & Arrays: Equivalent Code


#define N 20 int a[N], i, *p, sum=0; for (i=0; i<N; ++i) sum += a[i] ; #define N 20 int a[N], i, *p, sum=0; p=a; for (i=0; i<N; ++i) sum += p[i] ;

#define N 20 int a[N], i, *p, sum=0; for (i=0; i<N; ++i) sum += *(a+i) ;

#define N 20 int a[N], i, *p, sum=0; for (p=a; p<&a[N]; ++p) sum += *p ;

17-02-03

P.P.Chakrabarti, IIT Kharagpur

You might also like