Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write an algorithm that finds both the smallest and largest numbers in a list of

ID: 440839 • Letter: W

Question

write an algorithm that finds both the smallest and largest numbers in a list of n numbers. try to find a method that does atmost 1.5n comparisions of array items.

Explanation / Answer

I have done this for 10 element. this is recursive program which takes less than 1.5 comparision ----------------------------------------------------------------- #include void minmax (int* a, int i, int j, int* min, int* max) { int lmin, lmax, rmin, rmax, mid; if (i == j) { *min = a[i]; *max = a[j]; } else if (j == i + 1) { if (a[i] > a[j]) { *min = a[j]; *max = a[i]; } else { *min = a[i]; *max = a[j]; } } else { mid = (i + j) / 2; minmax(a, i, mid, &lmin, &lmax); minmax(a, mid + 1, j, &rmin, &rmax); *min = (lmin > rmin) ? rmin : lmin; *max = (lmax > rmax) ? lmax : rmax; } } void main () { int a [] = {3, 4, 2, 6, 8, 1, 9, 12, 15, 11}; int min, max; minmax (a, 0, 9, &min, &max); printf ("Min : %d, Max: %d ", min, max); } -----------------------------------------------------------