只使用指针和动态内存分配的冒泡排序 [英] Bubble sort using only pointers and dynamic memory allocation
本文介绍了只使用指针和动态内存分配的冒泡排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试使用指针和动态内存分配来运行泡沫排序技术,但是代码似乎没有运行(eclipse崩溃)。请帮忙。我发布的代码如下:
I am trying to run Bubble Sort Technique using pointers and dynamic memory allocation, but the code doesn't seem to run (eclipse crashes). Please help. I am posting the code below:
#include<iostream>
using namespace std;
void sort(int *);
//===========================================
int main()
{
int *a = new int[5];
int *c = a;
cout << "Enter the numbers\n";
for(int i = 0; i < 5; i++)
{
cin >> *a;
a++;
}
a = a - 4;
sort(a);
cout << c;
cout<<"\nSorting complete";
cout<<"\n Array after sorting";
for(int i = 0; i < 5; i++)
{
cout << *c << endl;
c++;
}
delete []a;
delete []c;
a = c = NULL;
return 0;
}
//===========================================
void sort(int *a)
{
int *b = a + 1;
for(int i = 0; i < 5; i++)
{
if(*a > *b)
{
int temp = *a;
*a = *b;
*b = temp;
a++;
b++;
}
else
{
b++;
continue;
}
}
}
/ p>
Thank you in advance.
推荐答案
让生活更轻松
>
Get rid of code like
cin>>*a;
a++;
并使用
cin>>a[i];
别处使用相同的想法
这篇关于只使用指针和动态内存分配的冒泡排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文