You are on page 1of 6

Sorting Algorithms

Bubble Sort
Repeatedly steps through the list, comparing adjacent elements and swapping them if they are
in the wrong order. It keeps on going through the list until the whole list is ordered.

Starting from the first index, compare the


first and the second elements.If the first
element is greater than the second element,
they are swapped.
Now, compare the second and the third
elements. Swap them if they are not in order.
The above process goes on until the last
element.

The same process goes on for the remaining


iterations. After each iteration, the largest
element among the unsorted elements is
placed at the end.

In each iteration, the comparison takes place


up to the last unsorted element.

Algorithm for Bubble Sort

NumberOfItems <- 10
WHILE
NoMoreSwaps <- TRUE

FOR Pointer <- 1 TO NumberOfItems-1


IF NameList[Pointer] > NameList[Pointer+1]
THEN
NoMoreSwaps <- FALSE
Temp <- NameList[Pointer]
NameList[Pointer] <- NameList[Pointer+1]
NameList[Pointer + 1] <- Temp

Sorting Algorithms 1
ENDIF
ENDFOR
NumberOfItems <- NumberOfItems - 1
UNTIL NoMoreSwaps = TRUE

Insertion Sort

Suppose we need to sort the following array.

Initial array

1. The first element in the array is assumed to be sorted. Take the second element and store
it separately in key .Compare key with the first element. If the first element is greater
than key , then is placed in front of the first element.
key

Sorting Algorithms 2
If the first element is greater than key, then key is placed in front of the first element.

2. Now, the first two elements are sorted.Take the third element and compare it with the
elements on the left of it. Placed it just behind the element smaller than it. If there is no
element smaller than it, then place it at the beginning of the array.

Place 1 at the beginning

Sorting Algorithms 3
3. Similarly, place every unsorted element at its correct position.

Place 4 behind 1

Sorting Algorithms 4
Place 3 behind 1 and the array is sorted

Algorithm For Insertion Sort

FOR ThisPointer <- 2 to 10


// use a temporary variable to store item which is to
// be inserted into its correct location
Temp <- NameList[Pointer]
Pointer <- ThisPointer -1

WHILE (NameList[Pointer]>Temp) AND (POINTER>0)


//move list item to next location
NameList[Pointer +1] <- NameList[Pointer]
Pointer <- Pointer-1
ENDWHILE

//insert value of Temp in correct location


NameList[Pointer +1] <- Temp
ENDFOR

Sorting Algorithms 5
Python Code For Insertion Sort

def insertionSort(array):

for step in range(1, len(array)):


key = array[step]
j = step - 1

# Compare key with each element on the left of it until an element smaller than it is found
# For descending order, change key<array[j] to key>array[j].
while j >= 0 and key < array[j]:
array[j+1] = array[j]
j = j - 1

# Place key at after the element just smaller than it.


array[j + 1] = key

data = [9, 5, 1, 4, 3]
insertionSort(data)
print('Sorted Array in Ascending Order:')
print(data)

Sorting Algorithms 6

You might also like