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

Sorting Algorithms

The document describes 5 sorting algorithms: 1. Insertion sort works by iteratively inserting elements into a sorted subset from an unsorted subset in a similar way to arranging playing cards. It has low overhead and works well for small or nearly sorted data. 2. Selection sort works by repeatedly selecting the minimum element from the unsorted subset and inserting it into the sorted subset, minimizing swaps. It is ideal when swapping is expensive. 3. Bubble sort works by repeatedly comparing adjacent elements and swapping them if out of order, causing the largest/smallest element to "bubble" to the end with each pass through the list. It has quadratic time complexity. 4. Merge sort uses a divide-and-con
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Sorting Algorithms

The document describes 5 sorting algorithms: 1. Insertion sort works by iteratively inserting elements into a sorted subset from an unsorted subset in a similar way to arranging playing cards. It has low overhead and works well for small or nearly sorted data. 2. Selection sort works by repeatedly selecting the minimum element from the unsorted subset and inserting it into the sorted subset, minimizing swaps. It is ideal when swapping is expensive. 3. Bubble sort works by repeatedly comparing adjacent elements and swapping them if out of order, causing the largest/smallest element to "bubble" to the end with each pass through the list. It has quadratic time complexity. 4. Merge sort uses a divide-and-con
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

1.

Insertion Sort
- A straightforward sorting algorithm called insertion sort functions in a manner similar to
how you might arrange playing cards in your hands. In a sense, the array is divided into
sorted and unsorted parts. Values are chosen and assigned to the appropriate positions in
the sorted part of the data from the unsorted part. Because of its low overhead, insertion
sort is quick and most effective when the problem size is small or when the data is nearly
sorted (because it is adaptive).
-
2. Selection Sort

- The selection sort method sorts an array by repeatedly selecting the first element from
the unsorted portion that is the least in value while still taking into account ascending
order. In a given array, the method keeps two subarrays:

 The subarray which is already sorted


 The remaining subarray, which is unsorted

The smallest element (assuming ascending order) from the unsorted subarray is
selected and moved to the sorted subarray in each iteration or pass of the selection
sort. The property of the selection sort is to minimize the number of swaps. As a
result, it is the ideal option when exchanging is expensive.

3. Bubble Sort

- When using the sorting method Bubble Sort, adjacent elements that are in the wrong
order are frequently switched. The greatest element (in the case of ascending order) or
the smallest element (in the case of passing) reaches the finish after each iteration or
pass (in the case of descending order). Up until the list is sorted, the trip through the
list is repeated. This algorithm's average and worst-case complexity, where n is the
number of items, is O(n^2), making it unsuitable for huge data sets.

4. Merge Sort

- This sorting algorithm, in contrast to the first three, is based on the divide-and-
conquer strategy. The input array is split in half, calls are made for each half, and the
two sorted halves are then combined. The merge() function, which is used to combine
two halves, is the brain of the merge sort. The merge(A, p, q, r) key procedure merges
the two sorted sub-arrays into one, presuming that A[p..q] and A[q+1..r] are sorted.

When you require a stable and O(N log N) sort, merge sort is your only choice.

Merge () function

Out-of-place procedure is another name for the merge procedure.


5. Quick Sort

- Another Divide and Conquer algorithm is Quick Sort. It chooses one element to act as
the pivot and divides the supplied array around it, placing all of the smaller
components to the left of the pivot and all of the larger elements to the right. There
are other variations of quicksort that select pivots in various ways:

 Always pick the first element as a pivot.


 Always pick the last element as the pivot (implemented below).
 Pick a random element as a pivot.
 Pick the median as a pivot.

The partition() method in quicksort is the main operation. The goal of partitioning is
to sort an array, using element r as the pivot, and place all elements that are smaller
than r before r and all elements that are greater than r after r in the correct order. This
should all be completed in a straight line.

In comparison to the merge sort, the quicksort is the best method for small inputs.
Choose quicksort if you don't want a steady sort and average performance is more
important than worst performance. Let's first look at the partition algorithm and how
it is implemented.

Partition () algorithm

The index of components that are less (or equal to) the starting element on the right is
kept track of as r.

 We swap the elements of I and j and move the I pointer up if we discover an


element j that is less than r.
 If we discover an element j that is bigger than r, we simply move the j pointer up.

You might also like