Common Sorting and Searching Algorithms

LINEAR SEARCH

Algorithm

  1. Start
  2. Set count as zero, flag as zero, and position as zero
  3. Read the limit
  4. Read the elements
  5. Loop through the elements (i < n)
    1. Read the elements
  6. End of loop
  7. Print the elements
  8. Print “Enter the key element”
  9. Read the key element
  10. Loop through the elements (i < n)
    1. If (a[i] == key)
      1. Set flag = 1
      2. Set position = i + 1
      3. Increment count
      4. Print “Key found at position”
  11. If (flag == 1)
    1. Print “Key found, number of times”
  12. Else
    1. Print “Element not found”
  13. Stop

BINARY SEARCH

Algorithm

  1. Start
  2. Read the array size
  3. Read the array elements
  4. Loop (i < n)
    1. Read the array elements
  5. Loop ends
  6. Loop (i < n)
    1. Loop (j < n-1)
      1. If (a[i] > a[j+1])
        1. Set temp as a[i]
        2. Set a[i] as a[j+1]
        3. Set a[j+1] as temp
    2. Loop ends
  7. Loop ends
  8. Print sorted array
  9. Set begin as zero
  10. Set end as n
  11. Print “Enter the key”
  12. Read key
  13. Loop (begin < end)
    1. Set mid as (begin+end)/2
    2. If (a[mid]==key)
      1. Print “Element key found at position mid”
    3. If (key > a[mid])
      1. Set begin as mid + 1
    4. Else
      1. Set end as mid – 1
    5. If (begin <= end)
      1. Print “Not found”
  14. Stop

STACK USING ARRAY

Algorithm

  1. Start
  2. Set top to -1
  3. Print size of stack
  4. If (push)
    1. If (top == size – 1)
      1. Print overflow
    2. Else
      1. Decrement top
      2. Read one element
      3. Set stack[top] to element
  5. If (pop)
    1. If (top == -1)
      1. Print underflow
    2. Else
      1. Set item to stack[top]
      2. Set top to top – 1
  6. If (display)
    1. Set i as top
    2. Loop (i >= 0)
      1. Print stack[i]
      2. Decrement i
    3. End loop
  7. Stop

QUEUE USING ARRAY

Algorithm

  1. Start
  2. Set front and rear to -1
  3. Read size of queue
  4. If (insertion)
    1. If (rear == size – 1)
      1. Print overflow
    2. Else
      1. Print enter the element
      2. Read the element
      3. If (front == -1 & rear == -1)
        1. Set front = 0
        2. Increment rear
        3. Read element to queue[rear]
  5. If (deletion)
    1. If (front == -1)
      1. Print underflow
    2. Else
      1. Print deleted element is queue[front]
      2. If (front == rear)
        1. Set front and rear as -1
      3. Else
        1. Increment front
  6. If (display)
    1. If (front & rear are -1)
      1. Print underflow
    2. Else
      1. Set i as zero
      2. Loop while (i < rear)
        1. Print queue[i]
        2. Increment i
      3. End loop
  7. End

INSERTION SORT

Algorithm

  1. Start
  2. Read the array size
  3. Read the elements
  4. Loop (i < n)
    1. Read array elements
  5. Loop ends
  6. Set i to 1
  7. Loop (i < n)
    1. Set temp as a[i]
    2. Set j as i – 1
    3. Loop (a[i] > temp & j >= 0)
      1. Set a[j + 1] = a[j]
      2. Decrement j
    4. End loop
    5. Set a[j + 1] as temp
  8. End loop
  9. Loop (i < n)
    1. Print the sorted array
  10. Loop ends
  11. Stop

SELECTION SORT

Algorithm

  1. Start
  2. Read the size of the array
  3. Read the elements
  4. Set i to 0
  5. Loop(i<n)
    1. Read elements
    2. Increment i
  6. Loop ends
  7. Loop (i<n)
    1. Set j=i+1
    2. Loop (j<n)
      1. If(a[j]>a[i])
        1. Set temp=a[i]
        2. Set a[i]=a[j]
        3. Set a[j]=temp
    3. Loop ends
  8. Loop ends
  9. Loop (i<n)
    1. Print the sorted array
  10. Loop ends
  11. Stop

Quick Sort

Algorithm

  1. Start
  2. Define the quicksort function: quicksort(arr, low, high)
  3. If (low < high)
    1. Call partition function to get the pivot index: pivot_index = partition(arr, low, high)
    2. Recursively call quicksort on the left sub-array: quicksort(arr, low, pivot_index – 1)
    3. Recursively call quicksort on the right sub- array: quicksort(arr, pivot_index + 1, high)
  4. Define the partition function: partition(arr, low, high)
    1. Choose a pivot element, for example, the last element: pivot = arr[high]
    2. Initialize i as low – 1
    3. Loop from j = low to j < high
      1. If arr[j] is less than or equal to pivot
        1. Increment i and swap arr[i] with arr[j]
    4. Swap arr[i + 1] with arr[high] (or the pivot)
    5. Return i + 1 as the pivot index
  5. End

BUBBLE SORT

Algorithm

  1. Start
  2. Read the size of the array
  3. Read the elements
  4. Loop (i < n)
    1. Read the elements
  5. Loop ends
  6. Loop (i < n)
    1. Loop (j < n-1)
      1. If (a[i] > a[j+1])
        1. Set temp as a[i]
        2. Set a[j] as a[j+1]
        3. Set a[j+1] as temp
    2. If ends
    3. Loop ends
  7. Loop ends
  8. Print sorted array
  9. Loop (i < n)
    1. Print array
  10. Loop ends
  11. Stop