Pages

Monday 15 April 2013

Sorting algorithms


Sorting algorithms
Sorting data (placing data in some particular order e.g ascending or descending) will be same no matter which algorithm is used is used. The choice of algorithm should only affect the run time and memory usage of the program.


Selection sort
Is a simple but inefficient sorting algorithm. Th first iteration of the algorithm selects the smallest element and swaps it with first element in the array. The second iteration selects the second smallest item and swaps it with the second element in the array. The algorithm continues until the last iteration selects the second-largest element and replaces it with the second-to-last element, leaving the largest element at the last index of the array.
The selection sort algorithm runs in 0(n2) time.

Merge sort
This is a more efficient but complex sorting algorithm than selection and insertion sorts. The merge sort algorithm sorts an array by splitting an array into two equal sized  sub-array, sorting each sub-array and merging them into one larger array. This results in a pattern that is logarithmic log2 n levels. This results in a total efficiency of 0(n log n)



No comments:

Post a Comment