Sorting Algorithm in C-Bubble Sort and Selection Sort
Sorting Algorithm in C-Bubble Sort and Selection Sort
Sorting Algorithm in C-Bubble Sort and Selection Sort
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.
1 2 3 4 5 6
77 42 35 12 101 5
1 2 3 4 5 6
5 12 35 42 77 101
BUBBLE SORT
Oldest, most easiest, straightforward and simplistic
method of sorting data.
1 2 3 4 5 6
77 42 35 12 101 5
"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
1 2 3 4 5 6
42Swap77 12 101
77 42 35 5
"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
1 2 3 4 5 6
42 35Swap35
77 77 12 101 5
"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
1 2 3 4 5 6
42 35 12Swap12
77 77 101 5
"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
1 2 3 4 5 6
42 35 12 77 101 5
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
1 2 3 4 5 6
42 35 12 77 5 Swap101
101 5
"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
1 2 3 4 5 6
42 35 12 77 5 101
1 2 3 4 5 6
42 35 12 77 5 101
12 35 5 42 77 101
1 2 3 4 5 6
12 5 35 42 77 101
1 2 3 4 5 6
5 12 35 42 77 101
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 Sort
Selection sort determines the minimum (or maximum) of
the list and swaps it with the element at the index where
it is supposed to be.
1 2 3 4 5 6
77 42 35 12 101 5
Selection Sort
• Traverse a collection of elements
• Finding the first smallest element by scanning the
remaining elements
1 2 3 4 5 6
77 42 35 12 101 5
Selection Sort
• Traverse a collection of elements
• Finding the second smallest element by scanning the
remaining elements, and so on....
1 2 3 4 5 6
5 42 35 12 101 77
Selection Sort
• Traverse a collection of elements
1 2 3 4 5 6
5 12 35 42 101 77
Selection Sort
• Traverse a collection of elements
1 2 3 4 5 6
5 12 35 42 101 77
Selection Sort
• Traverse a collection of elements
1 2 3 4 5 6
5 12 35 42 101 77
Selection Sort
• Traverse a collection of elements
1 2 3 4 5 6
5 12 35 42 77 101
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.
Summary
The 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