Sorting Algorithms Exam: Questions, Answers, and Code Snippets, Exams of Mathematics

A comprehensive overview of sorting algorithms, including bubble sort, insertion sort, merge sort, quicksort, and heap sort. It features questions and answers related to algorithm execution, time complexity, and implementation in java. The document also includes code snippets and pseudocode for various sorting algorithms, making it a valuable resource for students studying data structures and algorithms. It covers both comparison-based and non-comparison-based sorting methods, such as bucket sort, counting sort, and radix sort, with explanations and examples.

Typology: Exams

2024/2025

Available from 09/26/2025

muriuki-meshack
muriuki-meshack šŸ‡ŗšŸ‡ø

1.2K documents

1 / 48

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
SORTING ALOGARITHMS EXAM LATEST VERSION -2025/2026-
100+ Q AND ANS ALL THE BEST
What is the number of different executions of algorithm?
A new execution can be triggered only by comparison.
Let's imagine that each comparison induces a 0 or 1 in a k-bit string. E.g. 0 if
a[i]<a[j] and 1 if a[i]>=a[j].
There are 2^k such strings, or possible executions.
Now, pigeons are n! possible inputs, holes are 2^k possible executions.
If 2^k < n!, then two different inputs would have just the same execution path of
the algorithm, which is not possible.
So:
2^k >= n!
n! = n (n-1) (n-2) ... 1
half of these numbers are >= n/2
n! >= (n/2)^(n/2)
2^k >= (n/2) ^ (n/2)
k >= (n/2) log(n/2) = theta(nlog(n))
Bucket sort, Counting sort, Radix Sort
non-comparison based algorithms
Bucket Sort:
used when sorting samples from uniform distribution on [0,1]
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30

Partial preview of the text

Download Sorting Algorithms Exam: Questions, Answers, and Code Snippets and more Exams Mathematics in PDF only on Docsity!

SORTING ALOGARITHMS EXAM LATEST VERSION - 2025/2026-

100+ Q AND ANS ALL THE BEST

What is the number of different executions of algorithm? A new execution can be triggered only by comparison. Let's imagine that each comparison induces a 0 or 1 in a k-bit string. E.g. 0 if a[i]<a[j] and 1 if a[i]>=a[j]. There are 2^k such strings, or possible executions. Now, pigeons are n! possible inputs, holes are 2^k possible executions. If 2^k < n!, then two different inputs would have just the same execution path of the algorithm, which is not possible. So: 2^k >= n! n! = n (n-1) (n-2) ... 1 half of these numbers are >= n/ n! >= (n/2)^(n/2) 2^k >= (n/2) ^ (n/2) k >= (n/2) log(n/2) = theta(nlog(n)) Bucket sort, Counting sort, Radix Sort non-comparison based algorithms Bucket Sort: used when sorting samples from uniform distribution on [0,1]

E.g. sorting array of size n. Pre-allocate k buckets with width 1/k In one pass on the array: if element is between 0 and 1/k - put it to 1st bucket, between 1/k and 2/k - 2nd bucket, and so on. Because input data is supposed to be distributed uniformly at random, the number of elements in each bucket will be extremely small, and you can use insertion sort to sort each bucket. motivation: nlogn algorithm that would work in-place: algorithm: QSort(array)

  1. find pivot p
  2. partition around the pivot - so that every element to the left is less than the pivot, and every element to the right is bigger then the pivot finds new pivot position k
  3. qsort(left from k), qsort(right from k) Partitioning routine: a) let p be the 1st element. If not - just swap (1st element, p) b) i = 1 - generic iterator k = 1 - left/right separator c) for i = 1 .. n- 1 if(a[i]<a[k]) swap(i,k) k++ i++

Calculate beginning and end indexes for elements with each value in resulting array. From end to beginning, start populating new array by putting elements to their buckets (also from end to beginning) Visualization: https://www.cs.usfca.edu/~galles/visualization/RadixSort.html Bubble Sort O(n^2) public void sort(List input) { int size = input.size(); for(int i = 0; i < size; i++){ for(int j = i+1; j <size;j++){ if(input.get(i) > input.get(j)){ swap(input, i ,j); } } } } number of comparisons: n + (n-1) + (n-2) + ... + 1 = n * (n-1)/2 = O(n^2) another implementation: public void sort(List input) { boolean swapped = false; int finish_index = input.size()-1; do{ swapped = false;

for(int i = 0; i < finish_index; i++){ if(input.get(i) > input.get(i+1)){ swap(input, i , i+1); swapped = true; } } finish_index--; }while (swapped); } Insertion Sort O(n^2) pseudocode: mark first element as sorted last_sorted_index=1; for each unsorted element:

  • extract the element
  • for i = last_sorted_index to 1 if a[i]>current element move it to the right by 1 else put element on i position code: public void sort(List input) {

Master Method Recursion in the form: T(n) <= a * T(n/b) + O(n^d) logical meaning of values: a - represents speed of subproblems peliferation b - represents speed of work-per-subproblem reduction O(n^2) Best case time complexity for selection sort? O(n^2) Average case time complexity for selection sort? O(n^2) Worst case time complexity for selection sort? O(n log n) Best case time complexity for quicksort? O(n log n) Average case time complexity for quicksort? O(n^2) Worst case time complexity for quicksort? O(n log n) Best case time complexity for merge sort? O(n log n) Average case time complexity for merge sort?

O(n log n) Worst case time complexity for merge sort? O(n) Best case time complexity for insertion sort? O(n^2) Average time complexity for insertion sort? O(n^2) Worst case time complexity for insertion sort? When the arrays is already sorted When does the best case of insertion sort happen? (O(n) time complexity) Arrays.sort() How to sort an array in Java? Arrays.sort(arr, new Comparator() { @Override public int compare(Integer o1, Integer o2) { return o2.compareTo(o1); } }) How to sort an array in desc order in Java? Collections.sort() How to sort a list in Java? Collections.sort(list, Collections.reverseOrder()) or with comparator

Write a code for selection sort public class Insertion { public static void sort(Comparable[] A) { for(int i = 0; i < A.length; i++) { for(int j = i; j > 0 && less(A[j], A[j - 1]); j--) { exch(A, j, j - 1); } } } public static void exch(Comparable[] A, int i, int j) { Comparable tmp = A[i]; A[i] = A[j]; A[j] = tmp; } public static boolean less(Comparable a, Comparable b) { return a.compareTo(b) < 0; } } Write a code for insertion sort

public static void mergeSort(int[] A) { int[] aux = new int[A.length]; mergeSort(A, aux, 0, A.length - 1); } private static void mergeSort(int[] A, int[] aux, int lo, int hi) { if(lo >= hi) return; int mid = lo + (hi - lo) / 2; mergeSort(A, aux, lo, mid); mergeSort(A, aux, mid + 1, hi); merge(A, aux, lo, mid, hi); } private static void merge(int[] A, int[] aux, int lo, int mid, int hi) { // copy to aux[] for(int i = lo; i <= hi; i++) { aux[i] = A[i]; } // merge back to A[] int i = lo; int j = mid + 1; for(int k = lo; k <= hi; k++) {

quickSort(A, lo, position - 1); quickSort(A, position + 1, hi); } private static int partition(int[] A, int lo, int hi) { int i = lo; int j = hi + 1; int k = A[lo]; while(true) { // find item on lo to swap while(A[++i] < k) { if(i == hi) break; } // find item on hi to swap while(A[--j] > k) { if(j == lo) break; } if(i >= j) break;

swap(A, i, j); } swap(A, lo, j); return j; } private static void swap(int[] A, int i, int j) { int tmp = A[i]; A[i] = A[j]; A[j] = tmp; } Write a code for quicksort public class BubbleSort { public static void sort(Comparable[] A) { for(int i = 0; i < A.length; i++) { for(int j = 0; j < A.length; j++) { if(less(A[i], A[j])) { swap(A, i, j); } }

// Traverse both array while (i<n1 && j <n2) { // Check if current element of first // array is smaller than current element // of second array. If yes, store first // array element and increment first array // index. Otherwise do same with second array if (arr1[i] < arr2[j]) arr3[k++] = arr1[i++]; else arr3[k++] = arr2[j++]; } // Store remaining elements of first array while (i < n1) arr3[k++] = arr1[i++]; // Store remaining elements of second array while (j < n2) arr3[k++] = arr2[j++]; }

Merge two sorted arrays Bubble Sort A Sorting Algorithm that passes over the list many times, compares each pair of adjacent items and swaps them if they are in the wrong order. Selection Sort An algorithm which passes over the list, finds the smallest item and moves it to the left, then repeats the exercise for the remaining list until it is all sorted. Quick Sort In this sorting algorithm, a pivot is chosen, and all the elements moved either side of the pivot. This is repeated with another pivot either side, recursively until done. Merge Sort A sorting algorithm that sorts partial lists then merges them together. Sorting Algorithm A process commonly used to sort data Ascending Rising, going from smallest to largest Descending Falling, going from largest to smallest List A set of data that can be sorted into order Array A variable that can hold list items Iteration A code construct, also known as a loop.

quick sort best case nlg(n) quick sort average case nlg(n) quick sort worst case n^ quick sort advantage on average, fastest known comparison-based sorting algorithm; i.e. the only faster algorithms we know of are designed for specific kinds of data, rather than working on anything we can compare. Both quicksort and mergesort have the same order of growth, but in terms of constant factors of that n * lg n term, quicksort's constants are lower. quick sort disadvantage the quadratic worst case. It's very unlikely to occur but it can happen, making quicksort not the best choice if you need to be at n * lg n time. quick sort space unstable partition takes o(1) at most o(logn) after partition Merge sort best case nlg(n) merge sort average case

nlg(n) merge sort worst case nlg(n) merge sort advantage n * lg n in all cases, even worst case merge sort disadvantage uses linear extra memory (the temporary array needed by merge)...this is especially a problem if you have a VERY large array to sort, since you might not even have enough memory left over to create another array that size. Also, not quite as fast as quicksort's average case, when you consider the constant factors (since they both have n * lg n order of growth) merge sort space o(n) insertion sort best case n insertion sort average case n^2 but half the time as worst case insertion sort worst case n^ insertion sort advantage works well for partially sorted or completely sorted arrays; also good for small arrays since quicksort and mergesort tend to be overkill for such arrays