64,646
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
const int MAXSIZE = 30;
void InsertSort(int (&array)[MAXSIZE],int left,int right)
{
for (int i=left+1;i<=right;i++) //这里改为for (int i=left+1;i<right;i++)也不能正确排序.
if (array[i]<array[i-1])
{
int temp = array[i], j = i-1;
do
{
array[i+1] = array[i];
j--;
}
while (j >= left && temp < array[i]);
array[j+1] = temp;
}
for (int k=0;k<MAXSIZE;k++)
{
std::cout<<array[k]<<" ";
if ((k+1)%10 == 0) std::cout<<std::endl;
}
}
int main(void)
{
int array[MAXSIZE];
srand(unsigned(time(NULL)));
for (int i=0;i<MAXSIZE;i++)
array[i] = rand()%10000;
InsertSort(array,0,MAXSIZE-1); //这里改为InsertSort(array,1,MAXSIZE)也不能正确排序.
return 0;
}
#include<iostream>
#include<time.h>
const int MAXSIZE = 30;
void InsertSort(int (&array)[MAXSIZE],int left,int right)
{
for (int i=left+1;i<=right;i++)
{
if (array[i] < array[i-1])
{
int temp = array[i], j = i-1;
do
{
array[j+1] = array[j];//修改
j--;
}while (j >= left && temp < array[j]);//修改
array[j+1] = temp;
}
}
for (int k=0;k<MAXSIZE;k++)
{
std::cout<<array[k]<<" ";
if ((k+1)%10 == 0) std::cout<<std::endl;
}
}
int main(void)
{
int array[MAXSIZE];
srand(unsigned(time(NULL)));
for (int i=0;i<MAXSIZE;i++)
array[i] = rand()%100;
InsertSort(array,0,MAXSIZE-1);
return 0;
}
#include<iostream>
#include<time.h>
const int MAXSIZE = 30;
void InsertSort(int (&array)[MAXSIZE],int left,int right)
{
for (int i=left+1;i<=right;i++) //这里改为for (int i=left+1;i<right;i++)也不能正确排序.
{
if (array[i] < array[i-1])
{
int temp = array[i], j = i-1;
do
{
array[j+1] = array[j];
j--;
}while (j >= left && temp < array[i]);
array[j+1] = temp;
}
}
for (int k=0;k<MAXSIZE;k++)
{
std::cout<<array[k]<<" ";
if ((k+1)%10 == 0) std::cout<<std::endl;
}
}
int main(void)
{
int array[MAXSIZE];
srand(unsigned(time(NULL)));
for (int i=0;i<MAXSIZE;i++)
array[i] = rand()%10000;
InsertSort(array,0,MAXSIZE-1); //这里改为InsertSort(array,1,MAXSIZE)也不能正确排序.
return 0;
}