here is a place to understand sort algorithms

Bubble Sort

Description

  • Bubble sorting starts with 2 first items. its compares items at index 0 and 1. for example in [7, 1, 4, 8, 9, 2] it compare 7 and 1.
  • 1 is smaller than 7 so they have to swap. then we have [1, 7, 4, 8, 9, 2]. that 1 and 7, 4, 8, 9, 2 that is a new unsorted list.
  • so again we compare 2 first items in our unsorted list that are 7 and 4. so 4 is smaller than 7, it has to swap with 7. then we have [1, 4, 7, 8, 9, 2]. that 1, 4 is and 7, 8, 9, 2 that is a new unsorted list.
  • then we compare 7 and 8, 7 is in right place. [1, 4, 7, 8, 9, 2]. then 8 and 9. also 8 is in a right place. [1, 4, 7, 8, 9, 2]. we compare 9 and 2. they have to swap [1, 4, 7, 8, 2, 9] so we get in the end of the array. we loop over again.
  • in every step we have to compare first two items in our new unsorted list. until the end of the array length. and start from array for sorting again. at the end we have [1, 2, 4, 7, 8, 9]
  • the name of bubble sort its because in every loop from first item to the end item of array, it bubbles up the biggest item in last index of array. in the example above in first loop we bubble up 9 in end of the array. in second loop we will bubble up 8 in the end of array before 9. and so on...

  • Big O: O(n^2)
Implimentation

Selection Sort

Description

  • We find and select minimum value and move it to the right place.
  • it means that in array [8, 2, 4, 1, 3] the minimum value is 1, and we should move it to index 0. so we swap with 8.
  • now we have [1, 2, 4, 8, 3]. that 1 is sorted list and 2, 4, 8, 3 is unsorted list. so we do first step again on our new unsorted list.
  • the minimum value is 2. its lower than other items in our new unsorted list so it is in right place.
    so we have 1,2 a sorted list and 4, 8, 3 new unsorted list. [1, 2, 4, 8, 3]
  • 3 is smaller item in our new unsorted list. it has to swap with first item in unsorted list that is 4. so we have 1, 2, 3 a sorted list again and 8, 4 a new unsorted list. [1, 2, 3, 8, 4]
  • just like steps above we get a sorted list. [1, 2, 3, 4, 8]

  • Big O: O(n^2)
Implimentation

Insertion Sort

Description

  • In insertion sort theory, we pick a number in our array. so lets assume that we have [8, 2, 4, 1, 3] so we pick 8 at index 0. we have one value that is sorted. [8, 2, 4, 1, 3]and 2, 4, 1, 3 are new unsorted list. we have again 2 in our new unsorted list. we pick 2 that is smaller than 8, so we have to shifting (not swapping). it means that we keep 2. then shift 8 into right. then insert 2 instead of 8. so we have [2, 8, 4, 1, 3]. now we have 2, 8 sorted and 4, 1, 3 new unsorted list.
  • next we have 4. we keep it. 4 is smaller than 8, so we shift 8 into right. 4 is not smaller than 2 so we replace 4 in index 1. [2, 4, 8, 1, 3]. we have 2, 4, 8 sorted list and 1, 3 new unsorted list.
  • now we have 1.we keep it. 1 is smaller than 8 we shift 8 to right. then 1 is smaller than 4. we shift 4 into right. then 1 is samller than 2, so we shift 2 to right. then we replace 2 in index 0. [1, 2, 4, 8, 3]. we have 1, 2, 4, 8 sorted list and 3 an unsorted list.
  • and like the steps above we replace 3 in index 2. [1, 2, 3, 4, 8].

  • Big O: O(n^2)
Implimentation

Merge Sort

Description

  • The concept of Merge sort is that we break the array and merge again. so lets assume that we have an array like [8, 2, 4, 1, 3] we break this array into 2 parts from middle of the array.
  • so we have [8, 2] in the left array and [4, 1, 3] in the right array.
  • then we break right array into two parts. [8] as the left array. and [2] right array.
  • so we have two arrays that have 1 item and by default its sorted. then we compare these 2 arrays and sort them. so we have [2, 8]
  • [4, 1, 3] we apply above parts for this array too.
  • and at the end we have [1, 2, 3, 4, 8]

  • Big O: O(nlogn)
Implimentation

Quick Sort

under constructor

Counting Sort

under constructor

Bucket Sort

under constructor