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

C notes

The document explains two sorting algorithms: Bubble Sort and Selection Sort. Bubble Sort repeatedly compares and swaps adjacent elements to sort an array in ascending order, requiring n-1 passes for n elements. Selection Sort finds the smallest element and swaps it into the correct position, continuing this process until the array is sorted.

Uploaded by

shreshth0904
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

C notes

The document explains two sorting algorithms: Bubble Sort and Selection Sort. Bubble Sort repeatedly compares and swaps adjacent elements to sort an array in ascending order, requiring n-1 passes for n elements. Selection Sort finds the smallest element and swaps it into the correct position, continuing this process until the array is sorted.

Uploaded by

shreshth0904
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

A given set of n elements provided in form of n array

with n number of elements. Bubble sort compares all


the elements one by one and sort them based on their
values. If the given array has to be sorted in ascending
order, then bubble sort will start by comparing the first
element of the array with the second element, if the
first element is greater than the second element it will
swap both the elements and then move on to compare
the second and the third element and so on. If we have
total n elements then we need to repeat this process
for n-1 times. For example:
[5,1,6,2,4,3]
1st cycle:
[5,1,6,2,4,3] {5>1, so interchange}
[1,5,6,2,4,3] {5<6, no interchange}
[1,5,6,2,4,3] {6>2, so interchange}
[1,5,2,6,4,3] {6>4, so interchange}
[1,5,2,4,6,3] {6>3, so interchange}
[1,5,2,4,3,6]
Similarly, after all the iterations the array gets sorted
This completes the first step of the bubble sort. If there
are n elements to be sorted then the process
mentioned above should be repeated n-1 times to get
required results.
Selection sort
It is conceptually the simplest sorting algorithm. This
algorithm will first find the smallest element in the
array and swap it with the element in the first position,
then it will find the second smallest element and swap
it with the element in the second position and it will
keep on doing this until the array is sorted. It is called
selection sorting because it repeatedly selects the next
smallest element and swaps it into the right place. For
example:
[3,6,1,8,4,5]
1st cycle:
[1,6,3,8,4,5] {1 was the smallest and hence got
swapped}
[1,3,6,8,4,5] {3 was the 2nd smallest and hence got
swapped}
[1,3,4,8,6,5] {4 was the 3rd smallest and hence got
swapped}
[1,3,4,5,6,8] {5 was the 4th smallest and hence got
swapped}
[1,3,4,5,6,8] {Process is complete}

Question: Write a program to sort an array using


selection sort algorithm.

Main()
{

You might also like