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

Sorting Algorithms2

This document discusses several common sorting algorithms: Bubble Sort, Selection Sort, Insertion Sort, Quick Sort, Merge Sort, Shell Sort, and Heap Sort. It provides brief descriptions of how each algorithm works by rearranging elements in an array or list into ascending or descending order.

Uploaded by

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

Sorting Algorithms2

This document discusses several common sorting algorithms: Bubble Sort, Selection Sort, Insertion Sort, Quick Sort, Merge Sort, Shell Sort, and Heap Sort. It provides brief descriptions of how each algorithm works by rearranging elements in an array or list into ascending or descending order.

Uploaded by

Andre Aurea
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

SORTING

ALGORITHMS
What is Sorting Algorithm?
SORTING ALGORITHMS
is used to rearrange a given array
or list of elements according to a
comparison operator on the
elements. The comparison operator
is used to decide the new order of
elements in the respective data
structure.
BUBBLE SORT
BUBBLE SORT
is a sorting algorithm that compares two
adjacent elements and swaps them until
they are in the intended order.

Just like the movement of air bubbles in


the water that rise up to the surface, each
element of the array move to the end in
each iteration.
6305
First
Pass:
6305 –> 36
05
3605 –> 3065
3065 –> 3056
3056
Second
Pass:3 0 5 6 –> 03
56
0356 –> 0356
0356
Third Pass:
0356 –> 03
56
SELECTION
SORT
SELECTION SORT
is a sorting algorithm that selects the
smallest or largest element from an
unsorted list in each iteration and places
that element at the beginning of the
unsorted list.
64 25 12 22 11
First
Pass:
64 25 12 22 11 –> 11 25 12 22 64
Second
Pass:
11 25 12 22 64 –> 11 12 25 22 64
Third
Pass:
11 12 25 22 64 –> 11 12 22 25
64
Fourth
Pass:
11 12 22 25 64 –> 11 12 22 25 64
Fifth Pass:
11 12 22 25 64 –> 11 12 22 25 64
INSERTION
SORT
INSERTION SORT
is a sorting algorithm that places an
unsorted element at its suitable place in
each iteration.

Insertion sort works similarly as we sort


cards in our hand in a card game.

We assume that the first card is already


12 11 13 5 6
First
Pass:
12 11 13 5 6 –> 11 12 13 5 6
Second
Pass:
11 12 13 5 6 –> 11 12 13 5 6
Third
Pass:
11 12 13 5 6 –> 11 12 5 13 6
11 12 5 13 6 –> 11 5 12 13 6
11 5 12 13 6 –> 5 11 12 13 6
Fourth Pass:
5 11 12 13 6 –> 5 11 12 6
13
5 11 12 6 13 –> 5 11 6 12
13
5 11 6 12 13 –> 5 6 11 12
13
5 6 11 12 13 –> 5 6 11 12
13
QUICK SORT
QUICK SORT
is a sorting algorithm based on the divide
and conquer approach.

An array is divided into subarrays by


selecting a pivot element (element
selected from the array).
MERGE SORT
MERGE SORT
a sorting algorithm that works by dividing
an array into smaller subarrays, sorting
each subarray, and then merging the
sorted subarrays back together to form
the final sorted array.
It can sort large arrays relatively quickly.

Merge sort is a popular choice for sorting


large datasets because it is relatively
efficient and easy to implement.
SHELL SORT
SHELL SORT
is an in-place comparison sort that is also
known as Shell.

is mainly a variation of Insertion Sort. In


insertion sort, we move elements only
one position ahead. When an element
has to be moved far ahead, many
movements are involved.
HEAP SORT
HEAP SORT
is a comparison-based sorting technique
based on Binary Heap data structure. It is
similar to the selection sort where we first
find the minimum element and place the
minimum element at the beginning.

You might also like