标签:
1.算法之选择排序
选择排序
int array[9] = {7, -5, 88, 33, 2, -99, 199, 88, -5};//定义一个无序的数组 int length = sizeof(array) / sizeof(array[0]); //选择排序 for (int i = 0; i < length - 1; i ++) { for (int j = i; j < length; j++) { if (array[i] > array [j]) { array[i] = array[i] ^ array[j]; array[j] = array[i] ^ array[j]; array[i] = array[j] ^ array[i]; } } } for (int i = 0; i < length; i ++) { printf("%d ",array[i]); } printf("\n"); return 0;
冒泡排序
int array[9] = {7, -5, 88, 33, 2, -99, 199, 88, -5}; int length = sizeof(array) / sizeof(array[0]); //冒泡排序 for (int i = 0; i < length - 1; i ++) { for (int j = 0; j < length - 1 - i; j ++) { if (array[j] > array[j + 1])//每次遍历,拿出前后两个数做比较 { array[j] = array[j] ^ array[j + 1]; array[j + 1] = array[j] ^ array[j + 1]; array[j] = array[j + 1] ^ array[j]; } } } for (int i = 0; i < length; i ++) { printf("%d ",array[i]); } printf("\n"); return 0;
标签:
原文地址:http://www.cnblogs.com/kk657483726/p/5133043.html