Algorithm SelectionSort
Input: n (size of the array), a[20] (array to be sorted)
Main Algorithm
Read n
Read elements into a
For i = 0 to n - 2
min = i
For j = i + 1 to n - 1
// Find the index of the minimum element in the unsorted part of the array
If a[j] < a[min]
min = j
// Swap the found minimum element with the first element of the unsorted part
temp = a[i]
a[i] = a[min]
a[min] = temp
End If
End For
End For
// Output the sorted array
Output "Sorted"
For i = 0 to n - 1
Output a[i]
End For
Algorithm BubbleSort
Input: a[] (array to be sorted), n (number of elements in the array)
Output: Sorted array in ascending order
For i = 0 to n - 1
For j = 0 to n - i - 1
If a[j] > a[j + 1]
// Swap if the order is broken
tmp = a[j]
a[j] = a[j + 1]
a[j + 1] = tmp
Algorithm main
Input: a[100] (array of integers), n, i
Output "Enter number of elements in the array:"
Read n
Output "Enter n integers"
For i = 0 to n - 1
Read a[i]
End For
// Perform bubble sort
BubbleSort(a, n)
Output "Printing the sorted array:"
For i = 0 to n - 1
Output a[i]
End For
Return 0
Algorithm MergeSort
Input: arr (array to be sorted), left (left index), right (right index)
Output: Sorted array arr[left..right]
Procedure divide(arr, l1, r1, l2, r2)
// Merge two sorted halves of the array
Initialize temp[10], i, j, k
i = l1
j = l2
k = 0
// Merge until one of the halves is exhausted
while (i <= r1) and (j <= r2)
if arr[i] < arr[j]
temp[k++] = arr[i++]
else
temp[k++] = arr[j++]
End While
// Copy the remaining elements from both halves
while i <= r1
temp[k++] = arr[i++]
while j <= r2
temp[k++] = arr[j++]
// Copy the merged elements back to the original array
for i = l1, j = 0; i <= r2; i++, j++
arr[i] = temp[j]
End For
End divide
Procedure conquer(arr, left, right)
// Recursively divide and conquer
If left < right
mid = (left + right) / 2
conquer(arr, left, mid)
conquer(arr, mid + 1, right)
divide(arr, left, mid, mid + 1, right)
End If
End conquer
Main Algorithm
Input: size, arr[10]
Read size
Read elements into arr
// Perform merge sort
conquer(arr, 0, size - 1)
// Output the sorted array
Output "After sorting..."
for i = 0 to size - 1
Output arr[i]
Output newline
Algorithm partition
Input: Integer array a[], Integer low, Integer high
Output: Integer
Integer i, j, temp, key
key = a[low]
i = low + 1
j = high
While True
While i < high and key >= a[i]
i = i + 1
End While
While key < a[j]
j = j - 1
End While
If i < j
temp = a[i]
a[i] = a[j]
a[j] = temp
Else
temp = a[low]
a[low] = a[j]
a[j] = temp
Return j
End If
End While
End Algorithm
Algorithm quicksort
Input: Integer array a[], Integer low, Integer high
Integer j
If low < high
j = partition(a, low, high)
quicksort(a, low, j - 1)
quicksort(a, j + 1, high)
End If
End Algorithm
Algorithm main
Input: n, a[20]
Output: SORTED ELEMENTS IN ASCENDING ORDER
Integer a[20], i, n
Output "Enter the value for n"
Read n
Output "Enter the elements"
For i = 0 to n - 1
Read a[i]
End For
quicksort(a, 0, n - 1)
Output "Sorted Array is"
For i = 0 to n - 1
Output a[i]
End For
End Algorithm