C++ C++;插入排序
我有一个叫做WordSort(worddata W[],int count)的函数,它由两个变量组成 1-worddata是在文件中保存给定单词信息的数组。count只是一个计数器变量,用于查看数组中的哪个字 读入该程序的words.txt文件将只是一个字符串C++ C++;插入排序,c++,insertion-sort,C++,Insertion Sort,我有一个叫做WordSort(worddata W[],int count)的函数,它由两个变量组成 1-worddata是在文件中保存给定单词信息的数组。count只是一个计数器变量,用于查看数组中的哪个字 读入该程序的words.txt文件将只是一个字符串 this is a list of words there are letters and numbers 23 people recommend this program. 功能如下: void WordSort (worddata
this is a list of words
there are letters and numbers
23 people recommend this program.
功能如下:
void WordSort (worddata W [], int count)
{
for (int i=1; i < count; i++)
{
for (int j=i; j > 0 && W[j-1].word > W[j].word; j--)
{
Swap(W[j], W[j-1]);
}
}
}
Swap假设是将每个元素与它前面的元素交换
我认为WordSort函数工作得很好,唯一缺少的是Swap函数。有谁能给我指出正确的方向,或者更好地向我解释插入排序吗?Swap应该是这样的——我不知道你的例子有多接近
void Swap (worddata & a, worddata & b)
{
worddata temp = a;
a = b;
b = temp;
}
Swap应该是这样的——我不知道你的例子有多接近
void Swap (worddata & a, worddata & b)
{
worddata temp = a;
a = b;
b = temp;
}
改用标准库。在您的循环中:
for (...)
{
std:swap(W[j], W[j-1]);
}
swap要求worddata类具有显式或隐式定义的复制构造函数和赋值运算符。请改用标准库。在您的循环中:
for (...)
{
std:swap(W[j], W[j-1]);
}
void insertion_sort()
{
/* Algorithm : Insertion Sort
* Coded by .
*/
int num;
/*
* Asking the User no of Integers he/she wants to enter
*/
cout << "Enter no of integers u want to enter: ";
cin >> num;
/* Creating an Array to store the integers*/
int s[num];
/*Taking Integers from the User */
for(int i = 0 ; i < num ; i++)
{
cout << "Integer " << i+1 << " is : ";
int x;
cin >> x;
s[i] = x;
}
/* The Magic of INSERTION SORT */
for(int j = 1 ; j <= (num-1) ; j++)
{
int key = s[j];
int k = j-1;
while(k >=0 && key <= s[k])
{
s[k+1] = s[k];
k = k - 1;
}
s[k+1]=key;
}
/*Printing Out the Sorted List */
cout << "The Sorted List is \n\n";
for(int i = 0 ; i < num ; i++)
{
cout << s[i] << " ";
}
}
swap要求worddata类具有显式或隐式定义的复制构造函数和赋值运算符。void insertion\u sort()
void insertion_sort()
{
/* Algorithm : Insertion Sort
* Coded by .
*/
int num;
/*
* Asking the User no of Integers he/she wants to enter
*/
cout << "Enter no of integers u want to enter: ";
cin >> num;
/* Creating an Array to store the integers*/
int s[num];
/*Taking Integers from the User */
for(int i = 0 ; i < num ; i++)
{
cout << "Integer " << i+1 << " is : ";
int x;
cin >> x;
s[i] = x;
}
/* The Magic of INSERTION SORT */
for(int j = 1 ; j <= (num-1) ; j++)
{
int key = s[j];
int k = j-1;
while(k >=0 && key <= s[k])
{
s[k+1] = s[k];
k = k - 1;
}
s[k+1]=key;
}
/*Printing Out the Sorted List */
cout << "The Sorted List is \n\n";
for(int i = 0 ; i < num ; i++)
{
cout << s[i] << " ";
}
}
{
/*算法:插入排序
*代码由。
*/
int-num;
/*
*询问用户想要输入的整数数量
*/
cout>num;
/*创建一个数组来存储整数*/
整数s[num];
/*从用户获取整数*/
for(int i=0;ivoid insertion\u sort()
{
/*算法:插入排序
*代码由。
*/
int-num;
/*
*询问用户想要输入的整数数量
*/
cout>num;
/*创建一个数组来存储整数*/
整数s[num];
/*从用户获取整数*/
for(int i=0;i#包括
使用名称空间std;
整数插入(整数arr[],整数大小_arr)
{
内部i、j、n、温度;
对于(i=1;i=0;j--)
{
如果(arr[j]>温度){
arr[j+1]=arr[j];
arr[j]=温度;
}
}
arr[j]=温度;
}
for(i=0;使用“for循环”进行IIN插入排序)(2次迭代)
#包括
使用名称空间std;
整数插入(整数arr[],整数大小_arr)
{
内部i、j、n、温度;
对于(i=1;i=0;j--)
{
如果(arr[j]>温度){
arr[j+1]=arr[j];
arr[j]=温度;
}
}
arr[j]=温度;
}
对于(i=0;Ii提供单词的例子?代码是什么?INT++A;和<代码> B= +; >不是有效的C++代码。这不是冒泡排序吗?这是真的代码吗?它编译吗?解释刚才交换应该完成什么。我假设加号是字母“t”,在<代码>交换< /代码>方法中,你可以吗?“Dead Dead”?“期望的结果”?<代码> INT+A;和 B= +; >不是有效的C++代码。这不是冒泡排序吗?这是真的代码吗?它是编译的吗?解释一下交换应该完成的是什么。我假设加号是字母“t”,在<代码>交换< /C>方法中,你能解释为什么WordDATA有吗?要在temp前面吗?我在没有它的情况下尝试了这个方法,但它无法编译,因为您需要一个“局部”变量,这是将“temp”声明为一个局部变量,您将在其中存储“a”,当然,worddata是数据类型…请注意,可能是“wordata”不同于int@nastalgia如果不编译,则可能是worddata
既不支持复制构造,也不支持赋值运算符(或两者都不支持?)。否则这应该可以正确编译。它可以编译并且工作得很好。谢谢@Gary Walker和朋友们-我现在对这段代码的理解好多了,它已经完成了。我认为使用std::swap更好。你能解释为什么worddata必须放在temp前面吗?我在没有它的情况下尝试过这段代码,但它不会编译,因为你需要一个“本地”变量,这是将“temp”声明为局部变量,您将在其中存储“a”,当然,worddata是数据类型…请注意,可能“wordata”与int@nastalgia如果不编译,则可能是worddata
既不支持复制构造,也不支持赋值运算符(或两者都不支持?)。否则,这应该可以正确编译。它编译得很好,运行得也很好。谢谢@Gary Walker和朋友们-我现在对这段代码的理解好多了,它已经完成了。我认为使用std::swap会更好。有趣的是,数组是从0到num定义的,但我们从1迭代到num-1。因为当j为0时k=j-1处的洞被堵住了,但是它对num+1整数有效吗?有趣的是,数组是从0到num定义的,但是我们从1迭代到num-1。因为当j为0时k=j-1处的洞被堵塞了,所以它对num+1整数有效吗?