Bubble Sorting is constantly compared. If it is small, it is exchanged. A simple sort is exchanged only after a loop is compared. The number of exchanges is less than that of Bubble sorting.
# Include <stdio. h> # include <stdlib. h> void swap (int arr [], int I, int j) {int temp; temp = arr [I]; arr [I] = arr [j]; arr [j] = temp;} void SelectSort (int arr [], int n) {int I; int j; int min; for (I = 0; I <n; I ++) {min = I; for (j = I + 1; j <n; j ++) {if (arr [min]> arr [j]) min = j;} if (I! = Min) swap (arr, I, min) ;}} void print (int arr [], int n) {int I; for (I = 0; I <n; I ++) {printf ("% d", arr [I]);} printf ("\ n");} int main () {int arr [] = {9, 1, 5, 8, 3, 7, 4, 6, 2}; int n = sizeof (arr) /sizeof (arr [0]); printf ("Before sorting: \ n"); print (arr, n); SelectSort (arr, n); printf ("after sorting: \ n "); print (arr, n); system (" pause "); return 0 ;}
This article is from "Li Haichuan" blog, please be sure to keep this source http://lihaichuan.blog.51cto.com/498079/1282213