0% found this document useful (0 votes)
6 views

Selection Sort and Bubble Sort_Arrays

Uploaded by

nareshm3225
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Selection Sort and Bubble Sort_Arrays

Uploaded by

nareshm3225
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Sorting Algorithm

A sorting algorithm is defined as an algorithm that


puts the elements of a list in a certain order, which
can be either numerical order or any user-defined
order.

• Selection
• Bubble
• Insertion
Selection Sort

• Selection sort is another algorithm that is


used for sorting. This sorting algorithm,
iterates through the array and finds the
smallest number in the array and swaps it
with the first element if it is smaller than
the first element. Next, it goes on to the
second element and so on until all
elements are sorted.
Selection sort algorithm
• The selection sort algorithm sorts an array by repeatedly
finding the minimum element (considering ascending
order) from unsorted part and putting it at the beginning.
The algorithm maintains two subarrays in a given array.
• 1) The subarray which is already sorted.
2) Remaining subarray which is unsorted.
• In every iteration of selection sort, the minimum element
(considering ascending order) from the unsorted subarray
is picked and moved to the sorted subarray.
• Following example explains the above steps:
Consider the following elements are to be sorted in ascending order-
6, 2, 11, 7, 5

The selection sort algorithm works as illustrated below-


Step 1: For i=0

Step 2: For i=1


Step 3: For i=2

Step 4: For i=3

Step 5: For i=4

Loop gets terminated as ‘i’ becomes 4.


The state of array after the loops are
finished is as shown-
Pictorial View
#include<stdio.h>
int main() if(A[i]>A[j])
{ {
int i, j, n, temp, A[25];
printf("How many numbers u are temp=A[i];
going to enter?: ");
scanf("%d",&n); A[i]=A[j];
printf("Enter %d elements: ",n);
// Loop to get the
elements stored in array A[j]=temp;
for(i=0;i<n;i++) }
}
scanf("%d",&A[i]); }
// Logic of selection sortprintf("Sorted elements: ");
algorithm for(i=0;i<n;i++)
for(i=0;i<n;i++) printf(" %d",A[i]);
{ return 0;
for(j=i+1;j<n;j++) }
{
// Logic of selection sort
algorithm for (i = 0 ; i < n-1 ; i++)
for(i=0;i<n-1;i++) {
{
for(j=i+1;j<n;j+ index = i;
+)
for(j = i+1 ; j < n ; j++)
{ {

if(A[i]>A[j]) if(A[j] < A[index])


{ index = j;
}
temp=A[i];
temp = A[i];
A[i]=A[j];
A[i] = A[index];

A[j]=temp; A[index] = temp;


}
} }
}
Bubble Sort
• Bubble sort is the simplest sorting algorithm that works by
repeatedly swapping the adjacent elements if they are in wrong
order.

• In bubble sorting, consecutive adjacent pairs of elements in the


array are compared with each other. If the element at the lower
index is greater than the element at the higher index, the two
elements are interchanged so that the element is placed before the
bigger one. This process will continue till the list of unsorted
elements exhausts.

• This procedure of sorting is called bubble sorting because elements


‘bubble’ to the top of the list. Note that at the end of the first pass,
the largest element in the list will be placed at its proper position
"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

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
42 Swap42
77 35 12 101 5
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

1 2 3 4 5 6

42 7735Swap35
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 12 Swap12
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 Swap 101
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

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

0 1 2 3 4 5

42 35 12 77 5 101

Largest value correctly placed


“Bubbling” All the Elements
0 1 2 3 4 5
42 35 12 77 5 101
0 1 2 3 4 5
35 12 42 5 77 101
0 1 2 3 4 5
12 35 5 42 77 101
0 1 2 3 4 5
12 5 35 42 77 101
0 1 2 3 4 5
5 12 35 42 77 101
Pictorial View
Working

• Example: First Pass:


( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1.
( 1 5 4 2 8 ) –> ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) –> ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not
swap them.
• Second Pass:
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 )
( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ), Swap since 4 > 2
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs
one whole pass without any swap to know it is sorted.
• Third Pass:
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
• Fourth Pass:
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
Bubble sort program
int main()
{
int array[100], n, i, j, temp;
printf("Enter number of elements\n");
scanf("%d", &n); printf("Sorted Array:\n");
printf("Enter %d Numbers:n", n); for(i = 0; i < n; i++)
for(i = 0; i < n; i++) printf("%d\n", array[i]);
scanf("%d", &array[i]); return 0;
for(i = 0 ; i < n - 1; i++) }
{
for(j = 0 ; j < n-i-1; j++)
{
if(array[j] > array[j+1])
{
temp=array[j];

array[j]=array[j+1];

array[j+1]=temp;
}
}
}

You might also like