You are on page 1of 28

SORTING

ALGORITHM
Data Structure
By. Erwin D. Marcelo
SORTING
• Sorting is a process of ordering or placing a list of elements from a
collection in some kind of order.
• Sorting can be done in increasing(ascending) and decreasing
(descending) order.
• By sorting data, it is easier to search through it quickly and easily.

• Following are some of the examples of sorting in real-life scenarios −


• Telephone Directory − The telephone directory stores the telephone numbers of
people sorted by their names, so that the names can be searched easily.
• Dictionary − The dictionary stores words in an alphabetical order so that
searching of any word becomes easy.
Important Terms
• Increasing Order
A sequence of values is said to be in increasing order, if the successive element is greater than
the previous one. For example, 1, 3, 4, 6, 8, 9 are in increasing order, as every next element is
greater than the previous element.

• Decreasing Order
A sequence of values is said to be in decreasing order, if the successive element is less than the
current one. For example, 9, 8, 6, 4, 3, 1 are in decreasing order, as every next element is less
than the previous element.

• Non-Increasing Order
A sequence of values is said to be in non-increasing order, if the successive element is less than
or equal to its previous element in the sequence. This order occurs when the sequence contains
duplicate values. For example, 9, 8, 6, 3, 3, 1 are in non-increasing order, as every next element is
less than or equal to (in case of 3) but not greater than any previous element.

• Non-Decreasing Order
A sequence of values is said to be in non-decreasing order, if the successive element is greater
than or equal to its previous element in the sequence. This order occurs when the sequence
contains duplicate values. For example, 1, 3, 3, 6, 8, 9 are in non-decreasing order, as every next
element is greater than or equal to (in case of 3) but not less than the previous one.
Sorting Algorithm/Technique
• Sorting algorithm specifies the way to arrange data in a particular order.

• Sorting technique depends on the situation. It depends on two


parameters.

1. Execution time of program that means time taken for execution


of program.
2. Space that means space taken by the program.

• Sorting techniques are differentiated by their efficiency and space


requirements.
Basic Sorting Algorithm
• BUBBLE Sort

• BALLOON Sort

• INSERTION Sort

• SELECTION Sort
swap
• Exchange the value

swap(a, b){
temp = a
a= b
b = temp
}
BUBBLE SORT
Bubble sort is a simple
sorting algorithm. This
sorting algorithm is
comparison-based
algorithm in which each
pair of adjacent
elements is compare and
the elements are
swapped if they are not
in order.
BALLOON SORT
The balloon sort is similar to the bubble
sort, in that it compares elements of the
array and swaps those that are not in their
proper positions. The difference between
these two sorts is the manner in which
they compare the elements. The balloon
sort compares the first element with each
following element of the array, making
any necessary swap. When the first pass
through the array is complete, the balloon
sort then takes the second element and
compares it with each following element
of the array swapping elements that are
out of order. This sorting process
continues until the entire array is ordered.
BALLOON SORT

27 70 19 9 3
0 1 2 3 4
BALLOON SORT

70 19 27 9 3
0 1 2 3 4

Compare 70 and 19, since 70 > 19 , then swap


BALLOON SORT

19 70 27 9 3
0 1 2 3 4

Compare 19 and 27, since 19 < 27 , then do nothing


BALLOON SORT

19 70 27 9 3
0 1 2 3 4

Compare 19 and 9, since 19 > 9 , then swap


BALLOON SORT

9 70 27 19 3
0 1 2 3 4

Compare 9 and 3, since 9 > 3 , then swap


BALLOON SORT

3 70 27 19 9
0 1 2 3 4
BALLOON SORT

3 70 27 19 9
0 1 2 3 4

Compare 70 and 27, since 70 > 27 , then swap


BALLOON SORT

3 27 70 19 9
0 1 2 3 4

Compare 27 and 19, since 27 > 19 , then swap


BALLOON SORT

3 19 70 27 9
0 1 2 3 4

Compare 19 and 9, since 19 > 9 , then swap


BALLOON SORT

3 9 70 27 19
0 1 2 3 4
BALLOON SORT

3 9 70 27 19
0 1 2 3 4

Compare 70 and 27, since 70 > 27 , then swap


BALLOON SORT

3 9 27 70 19
0 1 2 3 4

Compare 27 and 19, since 27 > 19 , then swap


BALLOON SORT

3 9 19 70 27
0 1 2 3 4

Compare 70 and 27, since 70 > 27 , then swap


BALLOON SORT

3 9 19 70 27
0 1 2 3 4

Compare 70 and 27, since 70 > 27 , then swap


BALLOON SORT

3 9 19 27 70
0 1 2 3 4

Array elements now are sorted


INSERTION SORT
• This is an in-place comparison-
based sorting algorithm. Here, a
sub-list is maintained which is
always sorted. For example, the
lower part of an array is maintained
to be sorted. An element which is
to be inserted in this sorted sub-list,
has to find its appropriate place and
then it has to be inserted there.
• The array is searched sequentially
and unsorted items are moved and
inserted into the sorted sub-list (in
the same array). 
• Selection sort is a simple sorting
algorithm. This sorting algorithm is an
SELECTION SORT in-place comparison-based algorithm in
which the list is divided into two parts,
the sorted part at the left end and the
unsorted part at the right end. Initially,
the sorted part is empty and the
unsorted part is the entire list.
• The smallest element is selected from
the unsorted array and swapped with
the leftmost element, and that element
becomes a part of the sorted array. This
process continues moving unsorted
array boundary by one element to the
right.

You might also like