drishh207 / Sorting-Algorithms

Bubble, Selection, Insertion, Shell, Merge, Quick and Radix Sort and their possible implementations.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Sorting-Algorithms

Bubble, Selection, Insertion, Shell, Merge, Quick and Radix Sort and their possible implementations.

To view the java files, follow the path: Soting-algorithms / src / algorthims

  1. Bubble Sort:
    One of the most easy and basic sorting alogrithms. Every element is compared with its adjacent element and swapped accordingly. It is an in-place algorithm.
    Time complexity: O(n^2) Worst case occurs when array is reverse sorted.

  2. Selection Sort:
    The array divides into sorted and unsorted portions. In every iteration, the smallest element is found from the unsorted portion. It is then swapped with the first index of unsorted portion.
    It is an in-place algorithm.
    Time Complexity: O(n^2) quadratic complexity, but involves less swapping than bubble sort.

  3. Insertion Sort:
    The array divides into sorted and unsorted portions. The first element of the unsorted potions is chosen and then inserted at the correct position in the sorted portion.
    It is an in-place algorithm.
    Time Complexity: O(n^2) quadratic complexity, and involves a lot of shifting of elements.

  4. Shell Sort:
    -It is a variation of insertion sort with an aim to reduce the shifting of elements. The value to be choosen from the unsorted portion is choosen with the largest gap and as the algorithm progresses, the gap reduces and finally becomes 1. The algo does some preliminary work(using gap values greater than 1) and then becomes insertion sort. By the time, it becomes insertion sort, the array is mostly sorted, so no shifting is needed.
    -One of the most efficient gap sequence is the knuth sequence. But for simplicity, gap = array.length / 2 is considered.
    -It is an in-place algorithm.
    -Time complexity depends on the gap values chosen. But worst case is O(n^2).
    -Better performance than insertion sort.

  5. Merge Sort:
    -It is a divide and conquer algorithm.
    -It has 2 phases:
        1. Splitting: Dividing the array till we get 1 element array.
        2. Merging: Merging these elements in the same order as they were divided in a soted manner.
    -Time Complexity: O(nlogn) It takes log(n+1) steps to divide the array and linear time O(n) to merge the 2 halves.
    -It is not an in-place algorithm.

  6. Quick Sort:
    -It is also based on divide and conquer rule but with 1 difference that the partitioning is done according to the pivot element chosen.The elements left to the pivot are less than it and to the right are greater then it. Therefore, the pivot comes to its correct position.
    -It is an in-place algorithm.
    -Time Complexity: Best and average case: O(nlogn)
         Worst case: O(n^2) Occurs mostly when a lot of duplicate elements are there in the array.

  7. Counting Sort:
    -In this sort, assumptions are made about the data. It works with non-negative discrete values within a specific range. So, floating point numbers and Strings cannot be sorted.
    -It doesnot use comparisons but counts the number of occurences of each value, stores them in a temporary array at correct indices of the numbers and then accordingly puts elements in the original array.
    -It is not an in-place algorithm.
    -Time Complexity: O(n) linear time complexity.

  8. Radix Sort:
    -Like counting sort, radix sort also makes assumptions about the data. The data must have same radix and width.
    -Radix: It is the number of unique digits or values in case of characters that a numbering system or an alphabet has.
    -Width: It is the number of digits or letters.
    -For e.g. radix of decimal numbers is 10.
        width of 12345 is 5.
    -Radix sort can be used to sort decimal and strings, not floating point numbers.
    -A stable sort is required at every stage. So counting sort is used.
    -The sorting is done first for digits at units place, then for ten's place and so on...
    -It is not an in-place algorithm since counting sort is used.
    -Time Complexity: O(n) because we are making assumptions about the data but runs slower than O(nlogn) algos due to overhead.

About

Bubble, Selection, Insertion, Shell, Merge, Quick and Radix Sort and their possible implementations.


Languages

Language:Java 100.0%