Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- void swap(int *a ,int *b){
- int temp = *a;
- *a = *b;
- *b = temp;
- }
- void selectionSort(int a[], int n){
- int i, j, min;
- for(i = 0; i < n - 1; i++){
- min = i;
- for(j = i + 1; j < n; j++)
- if(a[j] < a[min]) min = j;
- if(min != i) swap(&a[min], &a[i]);
- }
- }
- int main(){
- int *a, n, i;
- printf("Enter the number of elements: ");
- scanf("%d", &n);
- a = (int *) malloc(sizeof(int) *n);
- printf("Enter %d elements:-\n", n);
- for(i = 0; i < n; i++) scanf("%d", &a[i]);
- selectionSort(a, n);
- for(i = 0; i < n; i++) printf("\t%d\t", a[i]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement