-
Notifications
You must be signed in to change notification settings - Fork 42
Selection Sort #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Selection Sort #47
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| #include<stdio.h> | ||
| void main() | ||
| { | ||
|
|
||
| int a[20],i,j,n,temp,s,pos; | ||
| printf("Enter n"); | ||
| scanf("%d",&n); | ||
| printf("Enter elements in the array"); | ||
| for(i=0;i<n;i++) | ||
| scanf("%d",&a[i]); | ||
|
|
||
| /*using selection sort*/ | ||
| for(i=0;i<n;i++){ | ||
| s=a[i]; | ||
| pos=i; | ||
| for(j=i;j<n;j++){ | ||
| if(a[j]<s) | ||
| { | ||
| s=a[j]; | ||
| pos=j; | ||
| } | ||
| } | ||
| temp=a[i]; | ||
| a[i]=a[pos]; | ||
| a[pos]=temp; | ||
| } | ||
|
|
||
| printf("Sorted array is:\n"); | ||
| for(i=0;i<n;i++) | ||
| printf("%d ",a[i]); | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // C program for insertion sort | ||
| #include <math.h> | ||
| #include <stdio.h> | ||
|
|
||
| /* Function to sort an array using insertion sort*/ | ||
| void insertionSort(int arr[], int n) | ||
| { | ||
| int i, key, j; | ||
| for (i = 1; i < n; i++) { | ||
| key = arr[i]; | ||
| j = i - 1; | ||
|
|
||
| /* Move elements of arr[0..i-1], that are | ||
| greater than key, to one position ahead | ||
| of their current position */ | ||
| while (j >= 0 && arr[j] > key) { | ||
| arr[j + 1] = arr[j]; | ||
| j = j - 1; | ||
| } | ||
| arr[j + 1] = key; | ||
| } | ||
| } | ||
|
|
||
| // A utility function to print an array of size n | ||
| void printArray(int arr[], int n) | ||
| { | ||
| int i; | ||
| for (i = 0; i < n; i++) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can directly use |
||
| printf("%d ", arr[i]); | ||
| printf("\n"); | ||
| } | ||
|
|
||
| /* Driver program to test insertion sort */ | ||
| int main() | ||
| { | ||
| int arr[] = { 12, 11, 13, 5, 6 }; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. after formating your code with the online formatter. make sure your array should be as it is |
||
| int n = sizeof(arr) / sizeof(arr[0]); | ||
|
|
||
| insertionSort(arr, n); | ||
| printArray(arr, n); | ||
|
|
||
| return 0; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this include it's not required