0% found this document useful (0 votes)
3 views

sorting

Uploaded by

omraval0808
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

sorting

Uploaded by

omraval0808
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

NAME:RAVAL OM PRAGNESHBHAI

ENROLLMENT NUMBER:220280152057
TOPIC:Implementation and Time analysis of sorting
algorithms – Bubble sort, Selection sort, Insertion
sort, Merge sort and Quick sort.

 BUBBLE SORT:
-Bubble sort is a simple sorting algorithm that repeatedly steps through the list, compares
adjacent elements, and swaps them if they are in the wrong order. The pass through the list is
repeated until the list is sorted.

-Algorithm:
.Start with the first element (index 0) and compare it with the next element (index 1).
.If the first element is greater than the second element, swap them.
.Move to the next pair of elements and repeat the comparison and swap process.
.Continue this process until you reach the end of the list.
.After the first pass, the largest element will be at the end of the list.
.Repeat the process for the remaining unsorted elements until the entire list is sorted.

-TIme complexity:

The time complexity of bubble sort is O(n^2) in the worst and average cases. This is because,
in the worst case, you may need to make n passes through the list, and in each pass, you
make n comparisons and swaps.

In the best case, when the list is already sorted, the algorithm can have a linear time
complexity of O(n) since only one pass is needed, and no swaps are required.

-IMPLEMENTATION:

 SELECTION SORT
-Selection sort is another simple sorting algorithm that sorts an array by
repeatedly finding the minimum element from the unsorted part of the array and
putting it at the beginning.

-Algorithm:
Initial State:
-The array is divided into two parts: a sorted and an unsorted subarray.
The sorted subarray starts empty, and the unsorted subarray contains all the
elements.
Selection:
-Find the minimum element in the unsorted subarray.
-Swap the minimum element with the first element of the unsorted subarray.
Expansion of Sorted Subarray:
-The sorted subarray is expanded by one element (the minimum element).

Repeat:
-Repeat steps 2 and 3 until the entire array is sorted.

Time Complexity:

-The time complexity of selection sort is O(n^2) in the worst and average cases.
This is because, for each element in the unsorted subarray, the algorithm needs
to scan the remaining unsorted elements to find the minimum. This results in
roughly n/2 comparisons on average for each element.

-The best-case time complexity is also O(n^2), as even if the array is partially
sorted, the algorithm still needs to scan and compare elements in the unsorted
part.

 INSERTION SORT
-Insertion sort is a simple sorting algorithm that builds the final sorted array one item at a
time. It is much less efficient on large lists than more advanced algorithms such as
quicksort, heapsort, or merge sort

-Algorithm:

Initial State:
-The first element in the array is considered as a sorted subarray.
-The remaining elements are considered as an unsorted subarray.

Insertion:
-Take one element from the unsorted subarray and insert it into its correct position in the
sorted subarray.
-Move through the sorted subarray, shifting elements as necessary, until the correct
position is found for the element.

Repeat:
-Repeat step 2 for each element in the unsorted subarray until the entire array is sorted.

Time Complexity:

-The time complexity of insertion sort is O(n^2) in the worst and average cases. This is
because, for each element in the unsorted subarray, the algorithm may need to compare
and shift elements in the sorted subarray.

-The best-case time complexity is O(n) when the input array is already sorted, as the
algorithm only needs to compare each element once and doesn't need to perform any
shifts.

IMPLEMENTATION

 MERGE SORT
-Merge sort is a divide-and-conquer algorithm that recursively divides an array into halves, sorts each
half, and then merges them back together. It is known for its stable sorting and guarantees O(n log n)
time complexity.

Algorithm:
Divide:
-Divide the unsorted array into two halves.

Conquer:
-Recursively sort each half.

Merge:
-Combine the sorted halves back into a single sorted array.
-The merging process involves comparing elements from the two halves and placing them in the
correct order.

Repeat:
-Repeat steps 1-3 until the entire array is sorted.

TIME COMPLEXITY
-The time complexity of merge sort is O(n log n) in all cases—best, average, and worst. This is because
the array is repeatedly divided into halves, and each level of recursion processes all the elements in
the array, resulting in a total time complexity of O(n log n).

IMPLEMENTATION
 QUICK SORT
-Quick sort is another efficient sorting algorithm that uses a divide-and-conquer strategy. It
works by selecting a "pivot" element and partitioning the other elements into two sub-
arrays according to whether they are less than or greater than the pivot. The sub-arrays
are then recursively sorted.

Algorithm:

Choose Pivot:
-Select a pivot element from the array.

Partition:
-Rearrange the elements in the array so that elements smaller than the pivot come before
it, and elements greater than the pivot come after it.
-The pivot is now in its final sorted position.

Recursion:
-Recursively apply the quick sort algorithm to the sub-arrays on the left and right of the
pivot.

Repeat:
-Repeat steps 1-3 until the entire array is sorted.
Time Complexity:

-The average and best-case time complexity of quick sort is O(n log n), making it very
efficient for large datasets. However, in the worst case, the time complexity can be O(n^2)
if the pivot selection and partitioning consistently result in unbalanced sub-arrays.

IMPLEMENTATION:

You might also like