C 插入排序函数不使用';不行。为什么? void insertionsort(int a[],int n){ int-next,i,j; 对于(i=1;i
您也需要交换C 插入排序函数不使用';不行。为什么? void insertionsort(int a[],int n){ int-next,i,j; 对于(i=1;i,c,C,您也需要交换i-1索引值,并且第二个for循环条件应该是 void insertionsort(int a[], int n){ int next, i, j; for(i=1; i<n; i++){ if(a[i]<a[i-1]){ for(j=i-2; j>=0; j--){ if(a[j]<a[i]){ next = a[i];
i-1
索引值,并且第二个for循环条件应该是
void insertionsort(int a[], int n){
int next, i, j;
for(i=1; i<n; i++){
if(a[i]<a[i-1]){
for(j=i-2; j>=0; j--){
if(a[j]<a[i]){
next = a[i];
a[i] = a[j];
a[j] = next;
}
}
}
}
}
void insertionsort(int a[],int n){
int-next,i,j;
对于(i=1;ia[j]){
next=a[j-1];
a[j-1]=a[j];
a[j]=下一个;
}
}
}
}
}
<>代码> 您的解决方案不完全符合算法的状态。您可能会考虑不使用。
两个嵌套for循环:
void insertionsort(int a[], int n){
int next, i, j;
for(i=1; i<n; i++){
if(a[i]<a[i-1]){
for(j=i; j>0; j--){
if(a[j - 1]>a[ j ]){
next = a[ j - 1];
a[j - 1] = a[j];
a[j] = next;
}
}
}
}
}
void insertionsort(int a[],int n){
int i,key,j;
对于(i=1;i=0&&a[j]>键)
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=键;
}
}
现在正是学习的最佳时机。你所说的“它不起作用”具体是什么意思?
void insertionsort(int a[], int n){
int i, key, j;
for (i = 1; i < n; i++)
{
key = a[i];
j = i-1;
/*
Move all elements in the array at index 0 to i-1, that are
greater than the key element, one position to the right
of their current position
*/
while (j >= 0 && a[j] > key)
{
a[j+1] = a[j];
j = j-1;
}
a[j+1] = key;
}
}