Search and sort

IMPORTANT: bubble sort, quick sort is not in your syllabus dont read. here is the pdf link : https://pdflink.to/search-sort/ 1. Linear Search It is a searching technique that searches sequentially. Time Complexity: O(n) Algorithm: Linear_Searc...

Ayush Basak

IMPORTANT: bubble sort, quick sort is not in your syllabus dont read.

here is the pdf link :

https://pdflink.to/search-sort/


  • It is a searching technique that searches sequentially.

  • Time Complexity: O(n)

Algorithm: Linear_Search(A, n, key)

  1. Start from the first element of the array.

  2. Compare the key with the current element.

  3. If both are equal, the search is successful.

  4. If not equal, move to the next element.

  5. Repeat this process until the element is found or the array ends.

  6. If the full array is checked and no match is found, the search is unsuccessful.

Example

Array: A = [4, 2, 7, 9, 5]
Key: 9

  • Compare key with 4 → no

  • Compare with 2 → no

  • Compare with 7 → no

  • Compare with 9 → match found at position 4


  • It is a fast searching algorithm.

  • Time Complexity: O(log n)

  • Works only on sorted arrays.

Algorithm

Algorithm: Binary_Search(A, n, key)

  1. Set low = 1 and high = n.

  2. Find mid = (low + high)/2.

  3. Compare the key with A[mid].

  4. If they are equal → search successful.

  5. If key < A[mid], search in the left half (high = mid – 1).

  6. If key > A[mid], search in the right half (low = mid + 1).

  7. Repeat until low > high.

  8. If low > high → search unsuccessful.

Example

Array: A = [3, 6, 9, 12, 15]
Key: 9

  • low = 1, high = 5 → mid = 3

  • A[3] = 9 → match found at mid


3. Selection Sort

  • The list is divided into two parts: sorted + unsorted.

  • We repeatedly select the minimum from the unsorted part.

Algorithm: Selection_Sort(A, n)

  1. From the unsorted part, find the smallest element.

  2. Swap it with the first element of the unsorted part.

  3. Now the first element becomes part of the sorted list.

  4. Move to the next position and repeat the process.

  5. Continue until all elements are placed in sorted position.

Example

A = [7, 4, 9, 2]

  • Smallest element is 2 → swap with 7 → [2,4,9,7]

  • Next smallest in remaining → 4 → already in place

  • Next smallest → 7 → swap with 9 → [2,4,7,9]

Final sorted array → [2, 4, 7, 9]


5. Insertion Sort

  • Suitable only for small inputs.

  • Elements are inserted in the correct position in the sorted part of the list.

Algorithm: Insertion_Sort(A, n)

  1. Consider the first element as sorted.

  2. Take the next element as the key.

  3. Compare the key with elements on the left.

  4. Shift larger elements to the right.

  5. Insert the key into the correct position.

  6. Repeat for all elements.

Example

A = [5, 3, 8, 2]

  • Key = 3 → insert before 5 → [3,5,8,2]

  • Key = 8 → stays → [3,5,8,2]

  • Key = 2 → shift 8,5,3 → insert → [2,3,5,8]

Final sorted array → [2, 3, 5, 8]


6. Merge Sort

  • Divide and conquer technique.

  • Time Complexity: O(n log n)

Algorithm: Merge_Sort(A)

  1. Divide the array into two halves.

  2. Continue dividing until each part has one element.

  3. Merge the parts back by comparing and placing elements in sorted order.

  4. Continue merging until the whole list is sorted.

Example

A = [6, 3, 9, 1]

Divide: [6,3] and [9,1]
→ [6] [3] → merge → [3,6]
→ [9] [1] → merge → [1,9]

Final merge → [3,6] + [1,9] → [1,3,6,9]


7. Quick Sort

  • Partitions array into small and large parts.

  • Time Complexity: O(n log n)

Partitioning uses:

  • pivot

  • left pointer

  • right pointer


8. Shell Sort

  • More efficient version of insertion sort.

  • Uses a "gap" to compare far elements.

  • Gaps reduce in each pass.

Algorithm

Algorithm: Shell_Sort(A, n)

  1. Choose a gap value (n/2).

  2. Compare elements that are ‘gap’ distance apart.

  3. Swap if needed.

  4. Reduce the gap (gap = gap/2).

  5. For gap = 1, perform insertion sort.

  6. Continue until the array becomes fully sorted.

Example

A = [10, 20, 15, 6, 80, 21, 8]
n = 7 → first gap = 3

Compare positions:
(0 & 3), (1 & 4), (2 & 5), (3 & 6)

After arranging with gap 3 → new arrangement
Next gap = 1 → final insertion-sort-like pass

Final sorted array → [6, 8, 10, 15, 20, 21, 80]