C语言 数组排序 打乱 查找
2015-02-24 11:52
211 查看
如有补充/优化 欢迎各路大神指点一二三四五六七......
本小菜刚踏入IT这个深坑针对前段时间学习有关C语言的数组部分知识自己总结了一下
同时反复敲,适合练手热身
冒泡排序
//此算法是对普通冒泡算法的优化,解决了特殊情况(当中途排序,出现后面的数刚好排完,而前面的数也已经排完,数组提前完成排序)下,避免代码重复运行,提高效率。
int array[] = {3, 5, 7, 2, 1, 9, 8, 6, 4};
int count = sizeof(array) / sizeof(array[0]);
int flag = 0;
//flag 为有序标志 0为无序 1为有序 为防止后面的数排完,前面也刚好排完,数组提前完成排序而引用该标志
for (int
i = 0; i < count -1 && flag ==0;
i++) {
flag = 1;
for (int j = 0; j < count - i - 1; j++) {
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
flag = 0;
}
}
}
for (int i = 0; i < count; i++) {
printf("%d\n", array[i]);
}
选择排序
int array[] = {1, 5, 3, 6, 8, 2, 4, 9, 7};
int count = sizeof(array) / sizeof(array[0]);
for (int i = 0; i < count - 1; i++) {
int minIndex = i;
for (int j = minIndex + 1; j < count; j++) {
if (array[minIndex] > array[j]) {
minIndex = j;
}
}
if (minIndex != i) {
int temp = 0;
temp = array[minIndex];
array[minIndex] = array[i];
array[i] = temp;
}
}
for (int i = 0; i < count; i++) {
printf("[%2d] : %d\n", i, array[i]);
}
插入排序
int array[] = {3, 5, 8, 2, 4, 9, 7, 1, 6};
int count = sizeof(array) / sizeof(array[0]);
for (int i = 1; i < count; i++) {
int j = i;
int temp = array[j];
while (j > 0 && temp < array[j - 1]) {
array[j] = array[j - 1];
j--;
}
array[j] = temp;
}
for (int i = 0; i < count; i++) {
printf("[%d] : %d\n", i, array[i]);
}
索引数组进行排序(模拟数据库的索引文件)
int array[] = {1, 3, 6, 12, 4, 15, 14, 5, 7, 8};
int brray[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int count = sizeof(array) / sizeof(array[0]);
for (int i = 0; i < count; i++) {
int minIndex = i;
for (int j = minIndex + 1; j < count; j++) {
if (array[brray[minIndex]] > array[brray[j]]) {
minIndex = j;
}
}
if (minIndex != i) {
int temp = brray[i];
brray[i] = brray[minIndex];
brray[minIndex] = temp;
}
}
for (int i = 0; i < count; i++) {
printf("%d \n", brray[i]);
}
折半查找
int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int count = sizeof(array) / sizeof(array[0]);
int target = 9;
int start = 0, end = count - 1, mid = (start + end) / 2;
while (start <= end) {
mid = (start + end) / 2;
if (array[mid] < target) {
start = mid + 1;
}
else if (array[mid] > target){
end = mid - 1;
} else {
break;
}
}
if (start <= end) {
printf("[%d] : %d\n", mid, array[mid]);
}else{
printf("not found");
}
顺序打乱
int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int count = sizeof(array) / sizeof(array[0]);
for (int i = count - 1; i > 0; i--) {
int rIndex = arc4random() % (i + 1);
if (rIndex != i) {
int temp;
temp = array[rIndex];
array[rIndex] = array[i];
array[i] = temp;
}
}
for (int i = 0; i < count; i++) {
printf("[%d] : %d\n", i, array[i]);
}
相关文章推荐
- c语言心得-----数组中对元素的操作排序,查找,插入,和删除
- 【c语言】二维数组中的查找,杨氏矩阵在一个二维数组中,每行都按照从左到右的递增的顺序排序,输入这样的一个数组和一个数,判断数组中是否包含这个数
- 【c语言】二维数组中的查找,杨氏矩阵在一个二维数组中,每行都依照从左到右的递增的顺序排序,输入这种一个数组和一个数,推断数组中是否包括这个数
- C语言//选择排序 //查找数组范围内最小的值跟第一个值交换 k记录最小值下标
- 题目:输入一个已经按升序排序过的数组和一个数字, 在数组中查找两个数,使得它们的和正好是输入的那个数字。 要求时间复杂度是O(n)。如果有多对数字的和等于输入的数字,输出任意一对即可。 例如输入数组1、2、4、7、11、15和数字15。由于4+11=15,
- (08) [转载]- 在排序数组中查找和为给定值的两个数字
- C语言面试题 4 (查找整数数组中第二大的数)
- 斜形数组排序(C语言)
- 程序员面试题精选100题(10)-在排序数组中查找和为给定值的两个数字
- C语言实现数组二分查找与顺序查找
- 在排序数组中查找和为给定值的两个数字
- 数组合并,排序,和查找
- 程序员面试题精选(10)-在排序数组中查找和为给定值的两个数字
- C#数组查找与排序
- 每天一道算法题8 在排序数组中查找和为给定值的两个数字
- 程序员面试题精选100题(10)-在排序数组中查找和为给定值的两个数字
- C语言中查找数组中的元素(WIN)
- 程序员面试题精选(10):在排序数组中查找和为给定值的两个数字
- 【我解C语言面试题系列】012 查找整数数组中第二大的数
- C#数组查找与排序