This document summarizes several sorting algorithms: bubble sort, insertion sort, selection sort, quicksort, merge sort, and radix sort. For each algorithm, it provides a high-level description of the approach, pseudocode for the algorithm, and time complexities. The key points are that sorting algorithms arrange data in a certain order, different techniques exist like comparing adjacent elements or dividing and merging, and the time complexities typically range from O(n log n) for efficient algorithms to O(n^2) for less efficient ones.
Sorting
• A sortingalgorithm is an algorithm used to generate the
ordered list in a certain order.
• Sorting is defined as ordering of given data based on
different sorting technique
Unsorted list
Sorted list
Bubble Sort
✔
Bubble sort,is referred as sinking sort it is the simple sorting algorithm.
✔
Swapping with adjustment element in the list. Until no further swapping is
needed which denoted all the elements as sorted in the list
✔
The sorting order is small element to largest number like the bubble in the
given image
5.
Algorithm
procedure bubbleSort( A: list of sortable items ) defined as:
do
swapped := false
for each i in 0 to length( A ) - 1 do:
if A[ i ] > A[ i + 1 ] then
swap( A[ i ], A[ i + 1 ] )
swapped := true
end if
end for
while swapped
end procedure
Insertion Sort
All thedata items arranged in one at a time
using Insertion sort algorithm
13.
Algorithm
insertionSort(array A)
for i= 1 to length[A]-1 do
begin
value = A[i]
j = i-1
while j >= 0 and A[j] > value do
begin
swap( A[j + 1], A[j] )
j = j-1
end
A[j+1] = value
end
Algorithm
for i =1:n,
k = i
for j = i+1:n, if a[j] < a[k], k = j
→ invariant: a[k] smallest of a[i..n]
swap a[i,k]
→ invariant: a[1..i] in final position
end
Quick Sort
• Elementis picked is knows as pivot element .
partitions the given array based on pivot
element
There are many different versions of quickSort
that pick pivot in different ways.
1.Always pick first element as pivot.
2.Always pick last element as pivot (implemented
below)
3.Pick a random element as pivot.
4.Pick median as pivot.
22.
Algorithm
function partition(array, left,right, pivotIndex)
pivotValue := array[pivotIndex]
swap array[pivotIndex] and array[right] // Move pivot to
end storeIndex := left
for i fromleft to right ? 1
if array[i] ? pivotValue
swap array[i] and array[storeIndex]
storeIndex := storeIndex + 1
swap array[storeIndex] and array[right] // Move pivot to its final
place return storeIndex
23.
Algorithm
procedure quicksort(array, left,right)
if right > left
select a pivot index (e.g. pivotIndex := left)
pivotNewIndex := partition(array, left, right,
pivotIndex) quicksort(array, left, pivotNewIndex - 1)
quicksort(array, pivotNewIndex + 1, right)
Merge Sort
“MergeSort isbased on Divide and Conquer algorithm.
It divides given input array in to two halves, calls
itself for the two halves and then merges the two
sorted halves.”
30.
Algorithm
function mergesort(m)
var listleft, right, result
if length(m) ? 1
return m
var middle = length(m) / 2
for each x in m up to middle
add x to left
for each x in m after middle
add x to right
left = mergesort(left)
right = mergesort(right)
result = merge(left, right)
return result
31.
Algorithm
function merge(left,right)
var listresult
while length(left) > 0 and length(right) >
0 if first(left) ? first(right)
append first(left) to
result left = rest(left)
else
append first(right) to
result right = rest(right)
end while
if length(left) > 0
append rest(left) to result
if length(right) > 0
append rest(right) to result
return result
Radix Sort
“Radix sortis a non-comparative integer sorting
algorithm that sorts data with integer keys by
grouping keys by the individual digits which
share the same significant position and value”