forked from Anuja-19/Snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Jasneet410/Jasneet410-patch-1
Selection_Sort
- Loading branch information
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include<stdio.h> | ||
int main(){ | ||
/* Here i & j for loop counters, temp for swapping, | ||
* count for total number of elements, number[] to | ||
* store the input numbers in array. You can increase | ||
* or decrease the size of number array as per requirement | ||
*/ | ||
int i, j, count, temp, number[25]; | ||
|
||
printf("Enter the number of elements : "); | ||
scanf("%d",&count); | ||
|
||
printf("Enter %d elements: ", count); | ||
// Loop to get the elements stored in array | ||
for(i=0;i<count;i++) | ||
scanf("%d",&number[i]); | ||
|
||
// Logic of selection sort algorithm | ||
for(i=0;i<count;i++){ | ||
for(j=i+1;j<count;j++){ | ||
if(number[i]>number[j]){ | ||
temp=number[i]; | ||
number[i]=number[j]; | ||
number[j]=temp; | ||
} | ||
} | ||
} | ||
|
||
printf("Sorted elements: "); | ||
for(i=0;i<count;i++) | ||
printf(" %d",number[i]); | ||
|
||
return 0; | ||
} |