SORTING ALGORITHM IN C- BUBBLE SORT AND SELECTION SORT
Nov 22, 2014
SORTING ALGORITHM IN C-
BUBBLE SORT AND SELECTION SORT
Contents
Introduction to Sorting
Bubble Sort
Selection Sort
Advantages
Disadvantages
Summary
Introduction To Sorting Sorting is a process in which records are arranged in
ascending or descending order.
Example: Telephone directory.
Sorting
• Sorting takes an unordered collection and makes it an ordered one.
512354277 101
1 2 3 4 5 6
5 12 35 42 77 101
1 2 3 4 5 6
BUBBLE SORTOldest, most easiest, straightforward and simplistic
method of sorting data.
Procedure: In this technique, the two successive elements A[i] and A[i+1] are exchanged whenever A[i] >= A[i+1].
Bubble Sort Algorithmfor(i=1;i<n;i++){
for(j=0;j<n-i;j++){if(a[j]>=a[j+1]){exchange (a[j],a[j+1])}}
}
BUBBLE SORT“Bubbling Up" the Largest Element
• Traverse a collection of elements–Move from the front to the end– “Bubble” the largest value to the end using pair-
wise comparisons and swapping
512354277 101
1 2 3 4 5 6
"Bubbling Up" the Largest Element
• Traverse a collection of elements–Move from the front to the end– “Bubble” the largest value to the end using pair-
wise comparisons and swapping
512354277 101
1 2 3 4 5 6Swap42 77
"Bubbling Up" the Largest Element
• Traverse a collection of elements–Move from the front to the end– “Bubble” the largest value to the end using pair-
wise comparisons and swapping
512357742 101
1 2 3 4 5 6Swap35 77
"Bubbling Up" the Largest Element
• Traverse a collection of elements–Move from the front to the end– “Bubble” the largest value to the end using pair-
wise comparisons and swapping
512773542 101
1 2 3 4 5 6Swap12 77
"Bubbling Up" the Largest Element
• Traverse a collection of elements–Move from the front to the end– “Bubble” the largest value to the end using pair-
wise comparisons and swapping
577123542 101
1 2 3 4 5 6
No need to swap
"Bubbling Up" the Largest Element
• Traverse a collection of elements–Move from the front to the end– “Bubble” the largest value to the end using pair-
wise comparisons and swapping
577123542 101
1 2 3 4 5 6Swap5 101
"Bubbling Up" the Largest Element
• Traverse a collection of elements–Move from the front to the end– “Bubble” the largest value to the end using pair-
wise comparisons and swapping
77123542 5
1 2 3 4 5 6
101
Largest value correctly placed
Items of Interest
• Notice that only the largest value is correctly placed
• All other values are still out of order• So we need to repeat this process
77123542 5
1 2 3 4 5 6
101
Largest value correctly placed
Repeat “Bubble Up” How Many Times?
• If we have N elements…
• And if each time we bubble an element, we place it in its correct location…
• Then we repeat the “bubble up” process N – 1 times.
“Bubbling” All the Elements
77123542 51 2 3 4 5 6
101
5421235 771 2 3 4 5 6
101
42 5 3512 771 2 3 4 5 6
101
42 35 512 771 2 3 4 5 6
101
42 35 12 5 771 2 3 4 5 6
101
N -
1
Advantages & Disadvantages of Bubble Sort
Advantages:very simple easy to program can be implemented in a short amount of time preferable when short lists are used.
Disadvantages: runs slowly and hence it is not efficient. Even if the elements are sorted, n-1 passes are required
to sort.
Selection SortSelection sort determines the minimum (or maximum) of
the list and swaps it with the element at the index where it is supposed to be.
Procedure : In this technique after obtaining the smallest element, it should be exchanged with the element in the ith position. temp = A[pos];A[pos] = A[i];A[i] = temp;
Selection Sort Algorithmfor(i=0;i<n-1;i++){ pos=i
for(j=i+1;j<n;j++){if(a[j]<a[pos])pos=j}temp=a[pos]a[pos]=a[i]a[i]=temp
}
Selection Sort
• Traverse a collection of elements• It works by first finding the smallest element using a
linear scan and swapping it into the first position in the list.
512354277 101
1 2 3 4 5 6
Selection Sort
• Traverse a collection of elements• Finding the first smallest element by scanning the
remaining elements
512354277 101
1 2 3 4 5 6
Selection Sort
• Traverse a collection of elements• Finding the second smallest element by scanning the
remaining elements, and so on....
771235425 101
1 2 3 4 5 6
Selection Sort
• Traverse a collection of elements
7735125 101
1 2 3 4 5 6
42
Selection Sort
• Traverse a collection of elements
7735125 101
1 2 3 4 5 6
42
Selection Sort
• Traverse a collection of elements
7735125 101
1 2 3 4 5 6
42
Selection Sort
• Traverse a collection of elements
10135125 77
1 2 3 4 5 6
42
Advantages & Disadvantages of Selection Sort
Advantages: Better to use when sorting through arrays. Performance is quicker than Bubble sort. Notable for its programming The simplest of sorting techniques and works very well for
small files
Disadvantages: Quite inefficient for sorting large data volumes. Spends most of its time trying to find the minimum
element in the "unsorted" part of the array.
SummaryThe choice of a sort algorithm normally bases on
some properties of the data you have to sort. Bubble sort is not a practical sorting algorithm
when n is large.Selection sort algorithm is an easy-to-program,
but very inefficient sorting algorithm.
References [1] Ashok N. Kamthane, Introduction to Data
Structures in C, Pearson Education, India, 2007 [2] A. M. Padma Reddy, Systematic Approach to Data
Structures Using C, 7th Edition, 2007 [3] Ellis Horowitz, Fundamentals of Computer
Algorithms, Universities Press, 2nd Edition, 2009
THANK YOU