实现数组插入排序并输出
2015-02-01 22:01
344 查看
实现排序输出
41
485
340
526
188
739
489
387
988
488
41
188
340
387
485
488
489
526
739
988
请按任意键继续. . .
#include<iostream> #include<cstdlib> using namespace std; class intarray { int *p;//指针指向首地址 int N;//确定有多少元素 public: intarray(int size) { N = size;//接受多少个元素 p = new int ; ; } void setnum(int id, int x) { *(p+id) = x; } void sort() { for (int i=0;i<N;i++) { for (int j=0;j<N-i-1;j++) { if (p[j]>p[j+1]) { int temp = p[j]; p[j] = p[j+1]; p[j+1] = temp; } } } } void printfAll() { cout<<endl; for (int *pa = p;pa<p+N;pa++) { cout<<*pa<<endl; } } }; int main() { intarray b(10); for (int i=0;i<10;i++) { b.setnum(i,rand()%999); } b.printfAll(); b.sort(); b.printfAll(); system("pause "); return 0; }输出结果:
41
485
340
526
188
739
489
387
988
488
41
188
340
387
485
488
489
526
739
988
请按任意键继续. . .
相关文章推荐
- 插入排序(Java实现)---从控制台输入不定长数组,并输出排序结果
- C#实现将一个数组中所有元素反序输出
- 数组顺时针旋转90度后输出(行列相等用单向链表实现)
- 编程:用java 语言实现,输入一个数,就相应地输出的几维数组!||用1、2、2、3、4、5这六个数字,用java写一个函数,打印出所有不同的排列,要求:"4"不能在第三位,"3"与"5"不能相连
- 编程题:用一组数组做函数参数来实现,输入两个数,输出其中最大数
- 编程题:用二维字符串数组实现。从键盘输入三个学生姓名,并输出。
- 实现统计数组中相同元素个数并输出
- 实现一个函数,传递一个有10个已经排好序的整形元素数组(数组大小实际为11)中,将一个指定的值按顺序插入,并在main函数中将插入后的数组输出。
- java类实现数组的五种排序 冒泡排序、选择排序、插入排序、希尔排序、数组排序
- 使用函数和指针实现数组的逆序输出
- 嘿牛程序员_成都传智博客_讨论字符串中字符出现的次数(续一:通过数组实现按次序输出)
- 数组输出函数的实现
- java实现递归将数组逆序输出
- java---实现数组的输出
- JavaScript实现数组全排列输出的代码
- 编程产生一个int数组,长度为30,并向其中随机插入1-30,并且不能重复输出数组。实现一个冒泡排序算法对其进行排序,输出排序结果
- Java数组反射实现动态的判断一个对象书否是数组,并且对数组进行拆包输出 。。
- 嘿牛程序员_成都传智博客_讨论字符串中字符出现的次数(续一:通过数组实现按次序输出)
- 写一主函数输入一数组,写一子函数实现对该数组的冒泡排序并输出。
- 螺旋输出一组数组数据的实现