Created
May 16, 2011 09:08
-
-
Save JulesWang/974124 to your computer and use it in GitHub Desktop.
Selection Sort
This file contains hidden or 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
/* | |
SELECTION-SORT(A) | |
1 for j ← 1 to length[A] - 1 | |
2 do min ← j | |
3 for i ← j+1 to length[A] | |
4 do if A[i] < A[min] | |
5 then min ← i | |
6 <> swap two numbers | |
7 tmp ← A[j] | |
8 A[j] ← A[min] | |
9 A[min] ← tmp | |
*/ | |
#include <stdio.h> | |
void SELECTION_SORT(int A[], int length) | |
{ | |
int j = 0; | |
int i = 0; | |
int min = 0; | |
int tmp = 0; | |
for(j=1; j<length-1; j++) | |
{ | |
min = j; | |
for(i=j+1; i<length; i++) | |
{ | |
if(A[i] < A[min]) | |
min = i; | |
} | |
tmp = A[j]; | |
A[j] = A[min]; | |
A[min] = tmp; | |
} | |
} | |
int main() | |
{ | |
int A[] = {0, 5, 2, 4, 6, 1, 3}; | |
int i = 0; | |
int length = sizeof(A)/sizeof(int); | |
SELECTION_SORT(A, length); | |
for(i=1; i<length; i++) | |
{ | |
printf("%d, ", A[i]); | |
} | |
getchar(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment