Vanshikapandey30 / Hacktoberfest2024

It's time to contribute to HacktoberFest 2024 :)
https://hacktoberfest.com
28 stars 63 forks source link

sorting in python #34

Closed Ankurcr7 closed 1 day ago

Ankurcr7 commented 1 day ago

Bubble sort

1. Starting from the first element, compare it with the next one.
2. If the first element is greater than the second, swap them.
3. Move to the next pair of adjacent elements, and repeat the comparison and swapping if necessary.
4. Continue this process for each pair of adjacent elements in the list.
5. At the end of each iteration, the largest unsorted element "bubbles up" to its correct position at the end of the list.
6. Repeat the entire process for the rest of the list, excluding the last sorted elements, until no more swaps are needed.

Insertion sort

1. Start from the second element (since the first element is trivially sorted).
2. Compare this element to the one before it.
3. If it is smaller, shift the preceding elements one position to the right until you find the correct spot for the current element.
4. Insert the element at the correct position.
5. Repeat the process for all elements until the entire array is sorted.

Selection sort

1. Start with the first element of the array.
2. Find the smallest element in the unsorted portion of the array.
3. Swap this smallest element with the first element of the unsorted portion.
4. Move the boundary between the sorted and unsorted portions one element to the right.
5. Repeat this process for each element until the entire array is sorted.

Quick sort

1. Choose a Pivot: Select a pivot element from the array. The pivot can be the first, last, middle, or a random element.
2. Partitioning: Rearrange the array such that:
i) Elements smaller than the pivot are moved to the left of the pivot.
ii) Elements larger than the pivot are moved to the right.
3. After partitioning, the pivot is in its final sorted position.
4. Recursion: Recursively apply the same process to the left and right sub-arrays (elements left of the pivot and elements right of the pivot).
5. The base case occurs when the sub-arrays have zero or one element, meaning they are already sorted.

Thank you.