C指针编程之道 ---第七次笔记

//指针在C语言算法中的应用

//首先说的是排序

//排序基本上分为5种

//插入排序, 选择排序, 交换排序, 归并排序, 分配排序

//先说7上8下的冒泡排序

#include <iostream>

#include <cstdio>

using namespace std;

void BubbleSort(int *Array, int n)

{

int a;

for(int i = n; i > 0; --i)

{

for(int j = 0; j < i; ++j)

{

if(*(Array + j) > *(Array + j + 1))

{

a = *(Array + j);

*(Array + j) = *(Array + j + 1);

*(Array +j + 1) = a;

}

}

}

}

int main()

{

int Array[8] = {5, 9, 2, 16, 7, 4, 12, 15};

printf("待排序数组为: \n");

for(int i = 0; i < 8; ++i)

printf("%d ", *(Array + i));

BubbleSort(Array, 8);

printf("\n冒泡排序后的数组是:\n");

for(int j = 0; j < 8; ++j)

{

printf("%d ", *(Array + j));

}

return 0;

}

//冒泡排序的改进1

//通过一般的冒泡排序,我们不难的得出一个结论,就是

//在一趟结束之后从r开始不再需要进行交换,那么就说明从Array[r + 1] 到Array[n - 1]已经排好序了

//从而只要是比较到r的位置就可以了。

#include <iostream>

#include <cstdio>

using namespace std;

void Bubble(int *Array, int n)

{

int bound = n;

int m, i;

int a;
//交换的中间变量

while(bound != 0)

{

m = 0;

for(i = 0; i < bound; i++)

{

if(*(Array + i) > *(Array + i + 1))

{

a = *(Array + i);

*(Array + i) = *(Array + i + 1);

*(Array + i + 1) = a;

m = i;

}

}

bound = m;

}

}

int main()

{

int Array[9] = {36, 80, 15, 26, 33, 2, 96, 46, 83};

int i;

printf("待排序的数组为:\n");

for(int i = 0; i < 9; ++i)

{

printf("%d ", *(Array + i));

}

Bubble(Array, 9);

printf("\n排序后的数组是:\n");

for(int j = 0; j < 9; ++j)

{

printf("%d ", *(Array + j));

}

return 0;

}

//冒泡排序改变2

//双向起泡

//就是每趟要找出最大的放在最下面找到最小的放在最上面。

//节省时间

#include <iostream>

#include <cstdio>

using namespace std;

void TwoBubble(int *Array, int n)

{

int boundmin = 0;

int boundmax = n;

int mmin, mmax;

int a;

//此使起泡重的下沉

while(boundmin < boundmax)

{

mmin = 0;

mmax = 0;

for(int i = boundmin; i < boundmax; i++)

{

if(*(Array + i) > *(Array + i + 1))

{

a = *(Array + i);

*(Array + i) = *(Array + i + 1);

*(Array + i + 1) = a;

mmax = i;

}

}

if(mmax == 0)

break;

boundmax = mmax;

for(int i = boundmax - 1; i > boundmin; i--)

{

if(*(Array + i) < *(Array + i - 1))

{

a = *(Array + i);

*(Array + i) = *(Array + i - 1);

*(Array + i - 1) = a;

mmin = i;

}

}

if(mmin == 0)

break;

boundmin = mmin;

}

}

int main()

{

int Array[9] = {36, 80, 14, 26, 33, 4, 96, 28, 83};

printf("待排序的数组为\n");

for(int i = 0; i < 9; ++i)

{

printf("%d ", *(Array + i));

}

TwoBubble(Array, 9);

printf("\n排序后的数组是:\n");

for(int j = 0; j < 9; ++j)

{

printf("%d ", *(Array + j));

}

return 0;

}

时间: 2024-08-11 04:16:43

C指针编程之道 ---第七次笔记的相关文章

C指针编程之道 ---第八次笔记

这次是算法中最常用的快速排序,常备成为快排,这里是头尾两个指针的算法. //快速排序 #include <iostream> #include <cstdio> using namespace std; int partition(int *Array, int i, int j) { int t = *(Array + 1); while(i < j) { while(i < j && *(Array + j) >= t) { j--; } if

C指针编程之道 ---第五次笔记

//数据结构中指针的应用 //内存中的堆栈和数据结构中的堆栈室友区别的 //在数据结构中,常常把堆栈放在一起表示的一种数据结构, //但是在内存中堆是存储动态的内存,栈是存放静态的以及调用的函数. //在数据结构中涉及了堆栈,队列,链表等在这里主要实现的是队列. //循环队列的指针应用 #include <iostream> #include <cstdio> #define QueueSize_UarLen 8 using namespace std; typedef struc

C指针编程之道 ---第四次笔记

//多为数组的指针学习 //定义二位数组 //int date[4][5]; //说明这个数组的所有成员都是int类型 //int date[4][5] = { // {1, 2, 3, 4, 5}, // {1, 2, 3, 4, 5}, // {1, 2, 3, 4, 5}, // {1, 2, 3, 4, 5} //}; //或者int date[4][5] = {1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5}; //例子访问二位数组 #include <

C指针编程之道 ---第三次笔记

这次整理的是函数指针和指针函数 这是指针的调用: 代码: #include <iostream> #include <cstdio> using namespace std; typedef unsigned char unit8_t; extern void swapdata(unit8_t dat_x, unit8_t dat_y); int main() { unit8_t x, y; scanf("%d %d", &x, &y); pri

C指针编程之道 ---第十一次笔记

这次来说交换函数的实现: 1. #include <stdio.h> #include <stdlib.h> void swap(int x, int y) { int temp; temp = x; x = y; y = temp; } int main() { int a = 10, b = 20; printf("交换前:\n a = %d, b = %d\n", a, b); swap(a, b); printf("交换后:\n a = %d,

C指针编程之道 ---第六次笔记

//指向文件类型的指针 //在C语言的文件你的读写一般使用系统的库函数来对数据进行读写 //文件的类型的指针 //文件的结构 #include <iostream> #include <cstdio> using namespace std; typedef struct { short level; //缓冲区满或者是空的程度 unsigned flags; //文件状态标志 char fd; //文件描述符 unsigned hold; //如无缓冲区不读取字符 short b

C指针编程之道 ---第一次笔记

//==================C指针编程之道===========================// /////////////第一次笔记//////////////// * 表示该变量为指针变量,这也是指针变量的特性. int *pStu; //定义指针变量pStu, 并且pStu指向int类型变量 static int *pStu; //定义指针变量pStu, 并且pStu指向静态整形变量 char *pStu;   //pStu是一个指针变量 取地址符& 和 取值运算符 * 通

C指针编程之道 ---第九次笔记

//这里说的是指针在算法中的应用 //直接选择排序 //每个排序的算法都是指针的方便性的特点来指向每个元素进行交换等 //这里的基本思想是对待排序的记录进行n - 1次选择. //第i次操作选择i大(小)的记录放在第i个(或者n - i - 1 个)位置上. //即每次都将一个记录放在它最终的位置上, //这就是所谓的"各回各家" #include <iostream> #include <cstdio> using namespace std; void Se

C指针编程之道 ---第十次笔记

//指针在搜索算法中实例 //迷宫算法 //搜索最长用到的就是深度优先搜索和广度优先搜索 //深度优先搜索就像名字一样,对每一个道路一直搜索到底, //为了防止思路,特别的设置了栈这种数据结构 //使得每次找到思路的时候还可以退出到出发点. // // //广度优先搜索 //广度优先搜索就是利用队列性质先进先出的性质,把每次的搜索结果放入队列, //排除思路等条件 // //回溯法 //就是枚举每个可能的判断,如果可以就执行,不可以就返回开始的地方 //八皇后的实现:回溯法 #include <