30
SORTING ALGORITHM IN C- BUBBLE SORT AND SELECTION SORT

Sorting

Embed Size (px)

DESCRIPTION

Introduction to sorting,Bubble Sorting,Selection Sorting,Comparision,Conclusion.

Citation preview

Page 1: Sorting

SORTING ALGORITHM IN C-

BUBBLE SORT AND SELECTION SORT

Page 2: Sorting

Contents

Introduction to Sorting

Bubble Sort

Selection Sort

Advantages

Disadvantages

Summary

Page 3: Sorting

Introduction To Sorting Sorting is a process in which records are arranged in

ascending or descending order.

Example: Telephone directory.

Page 4: Sorting

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

Page 5: Sorting

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].

Page 6: Sorting

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])}}

}

Page 7: Sorting

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

Page 8: Sorting

"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

Page 9: Sorting

"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

Page 10: Sorting

"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

Page 11: Sorting

"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

Page 12: Sorting

"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

Page 13: Sorting

"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

Page 14: Sorting

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

Page 15: Sorting

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.

Page 16: Sorting

“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

Page 17: Sorting

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.

Page 18: Sorting

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;

Page 19: Sorting

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

}

Page 20: Sorting

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

Page 21: Sorting

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

Page 22: Sorting

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

Page 23: Sorting

Selection Sort

• Traverse a collection of elements

7735125 101

1 2 3 4 5 6

42

Page 24: Sorting

Selection Sort

• Traverse a collection of elements

7735125 101

1 2 3 4 5 6

42

Page 25: Sorting

Selection Sort

• Traverse a collection of elements

7735125 101

1 2 3 4 5 6

42

Page 26: Sorting

Selection Sort

• Traverse a collection of elements

10135125 77

1 2 3 4 5 6

42

Page 27: Sorting

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.

Page 28: Sorting

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.

Page 29: Sorting

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

Page 30: Sorting

THANK YOU