0% found this document useful (0 votes)
36 views

JAVA Development: Extra: Sorting Algorithms

This document discusses and provides examples of insertion sort and merge sort algorithms. It shows the steps of insertion sort on an example array of numbers. It then summarizes the key steps of merge sort as: 1) divide the original array into smaller sub-arrays by taking the middle element, 2) recursively sort the sub-arrays, 3) merge the sorted sub-arrays back together by comparing elements and inserting them in order into a new array. Finally, it provides some extra reading resources on sorting algorithms.

Uploaded by

Cosmin Grosu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

JAVA Development: Extra: Sorting Algorithms

This document discusses and provides examples of insertion sort and merge sort algorithms. It shows the steps of insertion sort on an example array of numbers. It then summarizes the key steps of merge sort as: 1) divide the original array into smaller sub-arrays by taking the middle element, 2) recursively sort the sub-arrays, 3) merge the sorted sub-arrays back together by comparing elements and inserting them in order into a new array. Finally, it provides some extra reading resources on sorting algorithms.

Uploaded by

Cosmin Grosu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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