Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

JAVA Development

Extra: Sorting Algorithms


Insertion Sort

38, 27, 43, 3, 9, 82, 10 the initial array to sort

38, 27, 43, 3, 9, 82, 10 the first element is taken as the start

27, 38, 43, 3, 9, 82, 10 27 is moved on the first position, being smaller than 38

27, 38, 43, 3, 9, 82, 10 43 is kept on his position, being greater than 38

3, 27, 38, 43, 9, 82, 10 3 is inserted in the right position, the other elements are
shifted across to make room and keep the sort ordered
3, 9, 27, 38, 43, 82, 10 9 is inserted after 3, and all the rest of the numbers are
shifted across to keep the sort ordered
3, 9, 27, 38, 43, 82, 10 keep 82 on his position. No shifted needed

3, 9, 10, 27, 38, 43, 82 insert 10 on the right position and shift all the numbers
after it in order to keep the sort ordered
Binary search
Merge Sort

● known also as divide & conquer sorting algorithm


● divide the original data into smaller sets of data to solve the problem
● steps:
● take the element in the middle and split the collection into its left and right parts
● the resulting collections are recursively sorted in the same manner
● once this process is finished, then we need to combine the results
● for this, we take the results, compare elements to find the smallest one and
inserted it into the new collection, and so on
● the algorithm ends when all the collections have been inserted into the new
collection
Merge Sort
Merge Sort
Merge Sort
Merge Sort
Merge Sort
Extra reading:

https://www.geeksforgeeks.org/sorting-algorithms/#algo
https://visualgo.net/bn/sorting

You might also like