Sorting

Various types of sorting in swift.

Quick Sort

Logic: Create a pivot element & arrange elements with two partitions, If the elements are lesser than pivot add it to left partition & if elements bigger than the pivot arrange it to right side of partition & make call recursively with the subarray found with same approach. Time Complexity: Avg O(n^2) .. Best: O(n …

Quick Sort Read More »

Merge Sort

Merge Sort is based on divide & conquer approach, first we divide array into two parts until we got single element then merge the single single element and create a subarray, Now merge two sorted subarray into single array likewise in final we got the final sorted array. In Conquer approach we have i, j …

Merge Sort Read More »

Insertion Sort

# Approach Assume two part of the array sorted & unsorted. Sorted start from 1 element A[0] & fills with unsorted one by one (A[1] to A[n-1]) Attached code with dry run.  In Insertion sort we sort using two sets. One is Sorted set Another is unsorted one. Initially we sorted is index 0 element …

Insertion Sort Read More »

Selection Sort

Step 1: Find minimum element in array & swap with beginning element Step 2: So now swap min elements from inner loop in the array. So we have taken `minIndex` which assigned from 0th element i array till last & compare it with i+1 element