Sorting and Search Algorithms

Sorting Algorithm Functional Overview

  • Data retrieval efficiency depends significantly on whether data is sorted. Unsorted data requires more time and space to process compared to sorted data.
  • A successful search operation is referred to as retrieval, which returns the desired record or a pointer to it.
  • Failed searches result in a null record or a null pointer.

Merge Sort Algorithm

  • Operates on the divide and conquer principle by subdividing a list and sorting it recursively.
  • The midpoint is determined by the formula mid=(First+Last)/2mid = (First + Last) / 2.
  • Sub-lists A[First...mid]A[First...mid] and B[mid+1...Last]B[mid + 1...Last] are sorted separately and then combined to form the final sorted list.

Quick Sort Algorithm

  • Utilizes a pivot element vv to partition an unsorted list II into two sub-lists, I1I1 and I2I2.
  • Sub-list I1I1 contains keys smaller than the pivot, while I2I2 contains keys larger than the pivot.
  • The algorithm recursively sorts I1I1 and I2I2 into S1S1 and S2S2, then concatenates S1S1, vv, and S2S2 to produce the final list SS.

Bubble Sort Algorithm

  • Involves multiple sequential passes through a file.
  • In each pass, adjacent elements x[i]x[i] and x[i+1]x[i+1] are compared and interchanged if they are out of order.
  • The process iterates until the entire list is sorted, with the largest elements "bubbling" to their final positions in each pass.

Insertion Sort Algorithm

  • Each item from an unsorted list II is added to a sequence SS one at a time.
  • Items are inserted into SS at specific positions to maintain continuous sorted order.
  • The process repeats until the original list II is empty.

Selection Sort Algorithm

  • Scans an array of nn items to identify the smallest element, defined as Min[x[i]]Min[x[i]].
  • The smallest element is swapped into its correct place at the beginning of the current sub-array.
  • The algorithm then repeats this scan for the remaining n1n - 1 items until the entire list is sorted.

Heap Sort Algorithm

  • A specialized version of selection sort that utilizes a heap structure.
  • The largest element Max[X[i]]Max[X[i]] is removed from the heap and placed into a new sorted list.
  • This step repeats until all elements have been transferred from the heap to the new list.

Linear Search Algorithm

  • Used when the array size is known; the data does not require prior sorting.
  • The algorithm sequentially examines every index starting from counter=0counter = 0.
  • If the search element matches the list element at the current counter, that index is returned.
  • If the search concludes without a match, the value 1-1 is returned.

Binary Search Algorithm

  • Designed for high-speed retrieval from lists sorted in ascending or descending order.
  • The algorithm determines the middle position using mid=(first+last)/2mid = (first + last) / 2.
  • If the target is not at the midpoint, the search space is halved:
    • If the search element is less than the midpoint element, the new boundary becomes last=mid1last = mid - 1.
    • If the search element is greater than the midpoint element, the new boundary becomes First=mid+1First = mid + 1.

Questions & Discussion

  • Describe the following sort algorithms: Merge sort, Quick sort, Bubble sort, Insertion sort, Selection sort, and Heap sort.
  • Write down an algorithm for each of the sort types listed above.
  • State one limitation and one strength for each of the sorting algorithms.
  • Use the described algorithms to sort the following numerical lists:
    • 9,6,1,8,2,4,3,10,18,11,14,169, 6, 1, 8, 2, 4, 3, 10, 18, 11, 14, 16
    • 1000,700,1200,300,800,600,1700,1100,1300,20001000, 700, 1200, 300, 800, 600, 1700, 1100, 1300, 2000
  • Use the linear search algorithm to find the element 77 within the list 8,6,0,5,10,3,78, 6, 0, 5, 10, 3, 7.
  • Using the sorted list 3,4,7,9,12,20,36,38,403, 4, 7, 9, 12, 20, 36, 38, 40, perform a binary search for the element 3636.