COMPUTER STUDIES CONTENT
SORTING METHODS
MERGE SORT
Uses the divide, conquer and combine approach
Divide :Divides problem into a no. of sub-problems having smaller instances of the same
problem.
Conquer : Then we Conquer the sub-problems by solving them recursively.
Combine : Combines the solutions of the sub-problems into the solution for the original problem
Insertion sort
An insertion sort works by separating an array into two sections:
a sorted section and
an unsorted section.
Initially, of course, the entire array is unsorted
Insertion sort is a little bit difficult to explain in words.
It’s a bit easier to explain using an example.
The process then continues with 6. Each subsequent value in the unsorted section goes through
the same process until the entire array is in the correct order
Resembles the sarurawakokadeyadeya style
Selection sort
Idea:
Find the smallest element in the array
Exchange it with the element in the first position
Find the second smallest element and exchange it with the element in the second position
Continue until the array is sorted
For example, consider the following array, shown with array elements in sequence separated by
commas:
The leftmost element is at index zero, and the rightmost element is at the highest array index, in
our case, 4 (the effective size of our array is 5). The largest element in this effective array (index
0-4) is at index 2. We have shown the largest element and the one at the highest index in bold.
We then swap the element at index 2 with that at index 4. The result is:
We reduce the effective size of the array to 4, making the highest index in the effective array
now 3. The largest element in this effective array (index 0-3) is at index 1, so we swap elements
at index 1 and 3 (in bold):
The next two steps give us:
Disadvantage:
Running time depends only slightly on the amount of order in the file
Exchange sort/Bubble sort
Attempt to improve the ordering by comparing two elements (pairs) and interchange them if they
are not in order
The exchange sort compares the first element with each following element of the array, making
any necessary swaps.