Common Sorting and Searching Algorithms
Posted on May 2, 2024 in Computers
LINEAR SEARCH
Algorithm
- Start
- Set count as zero, flag as zero, and position as zero
- Read the limit
- Read the elements
- Loop through the elements (i < n)
- Read the elements
- End of loop
- Print the elements
- Print “Enter the key element”
- Read the key element
- Loop through the elements (i < n)
- If (a[i] == key)
- Set flag = 1
- Set position = i + 1
- Increment count
- Print “Key found at position”
- If (flag == 1)
- Print “Key found, number of times”
- Else
- Print “Element not found”
- Stop
BINARY SEARCH
Algorithm
- Start
- Read the array size
- Read the array elements
- Loop (i < n)
- Read the array elements
- Loop ends
- Loop (i < n)
- Loop (j < n-1)
- If (a[i] > a[j+1])
- Set temp as a[i]
- Set a[i] as a[j+1]
- Set a[j+1] as temp
- Loop ends
- Loop ends
- Print sorted array
- Set begin as zero
- Set end as n
- Print “Enter the key”
- Read key
- Loop (begin < end)
- Set mid as (begin+end)/2
- If (a[mid]==key)
- Print “Element key found at position mid”
- If (key > a[mid])
- Set begin as mid + 1
- Else
- Set end as mid – 1
- If (begin <= end)
- Print “Not found”
- Stop
STACK USING ARRAY
Algorithm
- Start
- Set top to -1
- Print size of stack
- If (push)
- If (top == size – 1)
- Print overflow
- Else
- Decrement top
- Read one element
- Set stack[top] to element
- If (pop)
- If (top == -1)
- Print underflow
- Else
- Set item to stack[top]
- Set top to top – 1
- If (display)
- Set i as top
- Loop (i >= 0)
- Print stack[i]
- Decrement i
- End loop
- Stop
QUEUE USING ARRAY
Algorithm
- Start
- Set front and rear to -1
- Read size of queue
- If (insertion)
- If (rear == size – 1)
- Print overflow
- Else
- Print enter the element
- Read the element
- If (front == -1 & rear == -1)
- Set front = 0
- Increment rear
- Read element to queue[rear]
- If (deletion)
- If (front == -1)
- Print underflow
- Else
- Print deleted element is queue[front]
- If (front == rear)
- Set front and rear as -1
- Else
- Increment front
- If (display)
- If (front & rear are -1)
- Print underflow
- Else
- Set i as zero
- Loop while (i < rear)
- Print queue[i]
- Increment i
- End loop
- End
INSERTION SORT
Algorithm
- Start
- Read the array size
- Read the elements
- Loop (i < n)
- Read array elements
- Loop ends
- Set i to 1
- Loop (i < n)
- Set temp as a[i]
- Set j as i – 1
- Loop (a[i] > temp & j >= 0)
- Set a[j + 1] = a[j]
- Decrement j
- End loop
- Set a[j + 1] as temp
- End loop
- Loop (i < n)
- Print the sorted array
- Loop ends
- Stop
SELECTION SORT
Algorithm
- Start
- Read the size of the array
- Read the elements
- Set i to 0
- Loop(i<n)
- Read elements
- Increment i
- Loop ends
- Loop (i<n)
- Set j=i+1
- Loop (j<n)
- If(a[j]>a[i])
- Set temp=a[i]
- Set a[i]=a[j]
- Set a[j]=temp
- Loop ends
- Loop ends
- Loop (i<n)
- Print the sorted array
- Loop ends
- Stop
Quick Sort
Algorithm
- Start
- Define the quicksort function: quicksort(arr, low, high)
- If (low < high)
- Call partition function to get the pivot index: pivot_index = partition(arr, low, high)
- Recursively call quicksort on the left sub-array: quicksort(arr, low, pivot_index – 1)
- Recursively call quicksort on the right sub- array: quicksort(arr, pivot_index + 1, high)
- Define the partition function: partition(arr, low, high)
- Choose a pivot element, for example, the last element: pivot = arr[high]
- Initialize i as low – 1
- Loop from j = low to j < high
- If arr[j] is less than or equal to pivot
- Increment i and swap arr[i] with arr[j]
- Swap arr[i + 1] with arr[high] (or the pivot)
- Return i + 1 as the pivot index
- End
BUBBLE SORT
Algorithm
- Start
- Read the size of the array
- Read the elements
- Loop (i < n)
- Read the elements
- Loop ends
- Loop (i < n)
- Loop (j < n-1)
- If (a[i] > a[j+1])
- Set temp as a[i]
- Set a[j] as a[j+1]
- Set a[j+1] as temp
- If ends
- Loop ends
- Loop ends
- Print sorted array
- Loop (i < n)
- Print array
- Loop ends
- Stop