Professional Documents
Culture Documents
TOPIC 10
SORTING
LEARNING OUTCOMES
• Sorting
• Bubble Sort
• Selection Sort
• Insertion Sort
• Quick Sort
• Merge Sort
SORTING
Sorting algorithm:
1. Internal sorting
All data to be sorted are loaded to RAM
2. External sorting
Sorting process using secondary storage
SORTING
o Simple:
• Bubble sort
• Selection sort
• Insertion sort
o Intermediate:
• Quick Sort
• Merge Sort
BUBBLE SORT
o Previous Bubble sort example has drawback: comparison still performed although all data fully
sorted.
o To fix this using ‘flag’ that tells if a search round have sorted the data. Also known as Bubble-
flag.
BUBBLE SORT
Algorithm:
for(i=0; i<N-1; i++){ /* N=number of data */
Set idx_smallest equal to i
for(j=i+1; j<N; j++){
If array[ j ] < array [ idx_smallest ] then idx_smallest = j
}
Swap array[ i ] with array[ idx_smallest ]
}
SELECTION SORT
SELECTION SORT
SELECTION SORT
Algorithm:
Algorithm:
73 71 76 72 75 78 79 77 74
J=2 K=
SWAP3
QUICK SORT
73 71 72 76 75 78 79 77 74
K=2 J=3
SWAP
73 71 72 76 75 78 79 77 74
K=2 J=3
SWAP
QUICK SORT
71 72 73 76 75 78 79 77 74 QS(0,0)
QS(2,1)
71 72 73 76 75 78 79 77 74 QS(3,8)
J=5
SWAP K=8
QUICK SORT
71 72 73 76 75 74 79 77 78
K=5 J=6
SWAP
71 72 73 74 75 76 79 77 78 QS(6,8)
K=8 J=?
SWAP
71 72 73 74 75 76 78 77 79 QS(6,7)
K=7 J=8
SWAP
QUICK SORT
71 72 73 74 75 76 77 78 79 QS(6,6)
R[0] R[1] R[2] R[3] R[4] R[5] R[6] R[7] R[8] QS(8,7)
QS(9,8)
71 72 73 74 75 76 77 78 79
QUICK SORT
QS(0,8)
QS(0,1) QS(3,8)
QS(6,6) QS(8,7)
QUICK SORT
void merge( int arr[], int L, int m1, int m2, int R )
{
int Lidx = L;
int Ridx = m2;
int Cidx = L;
int temparr[ SIZE ];
int i;
if ( Lidx == m2 ) {
while ( Ridx <= R )
temparr[ Cidx++ ] = arr[ Ridx++ ];
}
else {
while ( Lidx <= m1 )
temparr[ Cidx++ ] = arr[ Lidx++ ];
}
}
MERGE SORT
o In data processing often the data needs to be sorted (arranged) to facilitate the data process. The
data sorting process is called sorting. One of the goals of sorting is to speed up data search
(searching, retrieving).
o Based on the comparison of data values, sorting can be done in 2 ways Ascending and Descending.
o Bubble sort is a simple sorting technique and follows the principle of the bubble. Sorting in this
algorithm uses a comparison between 2 data.
o The Selection Sort algorithm divides the sorting process into rotation. In the first round selected data
with the smallest value and this data was placed in the smallest index position (data [0]).
o The insertion sort algorithm the process is the same as someone sorting the card. A piece of a card is
taken from a collection of cards and inserted in the right position.
o The Quick Sort algorithm works by dividing a collection of data into 2 parts in such a way that certain
elements (I) are right in their position, and all elements whose value is smaller than the Ine element is
on the left of the Ile element and all elements whose value is greater than the Ite element is to the
right of the Ine element.
o Merge Sort algorithm is sorting by merging. Two data sets, each of which has been sorted into being
combined into one. The merger begins by combining data groups with the smallest number of
elements.
ThankYOU...
REFERENCES
o Paul J. Deitel & Harvey. Deitel. (2022). C how to program. 09. Pearson Education. Hoboken.
ISBN:978-0-13-739839-3. Chapter 13
o Sorting Algorithm Animations: http://www.sorting-algorithms.com/
o Sorting Algorithms: http://www.youtube.com/watch?v=INHF_5RIxTE