Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 分拣,我该如何通过这一轮?_C++_Sorting - Fatal编程技术网

C++ 分拣,我该如何通过这一轮?

C++ 分拣,我该如何通过这一轮?,c++,sorting,C++,Sorting,我们的教授告诉我们要使我们的程序能够通过排序算法。。那我该怎么办 像这个^ 气泡排序码 这是对的吗???--> #include<stdio.h> int main() { int a[100],c[100],b,n,i,temp; printf("\t\t\tBUBBLE SORT\n"); printf("Enter total numbers : "); scanf("%d",&n); for(i=0;i<n;i++) { prin

我们的教授告诉我们要使我们的程序能够通过排序算法。。那我该怎么办

像这个^

气泡排序码

这是对的吗???-->

#include<stdio.h>
int main()
{
  int a[100],c[100],b,n,i,temp;
  printf("\t\t\tBUBBLE SORT\n");
  printf("Enter total numbers : ");
  scanf("%d",&n);
  for(i=0;i<n;i++)
  {
    printf("\nEnter %d number : ",i+1);
    scanf("%d",&a[i]);
    c[i]=a[i];
  }
  printf("\n");
  for(i=0;i<n;i++)
  {
    for(int j=0;j<n-1;j++)
    {
      b=0;
      if(a[j]<a[j+1])
      {   b++;
        temp=a[j];
        a[j]=a[j+1];
        a[j+1]=temp;
      }
      if(b!=0)
      {
        for(int k=0;k<n;k++)
        {
          printf("%d  ",a[k]);
        }
        printf("\n");
      }
    }
  }
  printf("\nAssending order By Bubble sort\n\t");
  for(i=0;i<n;i++)
  {
    printf("%d  ",c[i]);
  }
  return 0;
}

这只是一个简单的气泡排序问题。 只需要做两个函数,如bubble\u asc,bubble\u desc 并从主函数调用它

int main() {
...
bubble_asc();
bubble_desc();
...
}

void bubble_asc() {
...
do step by step bubble sort with ascending order here.
for(~~~~~) {
    for(~~~~~~) {
        if(array[a] < array[b])
             ....
        }
 }
 print arrays here..
}
void bubble_desc() {
...
do step by step bubble sort with descending order here.
for(~~~~~) {
    for(~~~~~~) {
        if(array[a] > array[b])
             ....
        }
 }
 print arrays here..
}

你的意思是黄色的表格是你想要的输出的一个例子吗?不完全是,就像这个{9,5,3,4,4,2}| | 5,3,4,2,9 | | 3,4,5,9 | | 3,2,5,9 | | | 2,3,4,5,9 | | |你能用眼睛一步一步地看代码吗?你能看到什么时候应该打印数组的内容吗?嗯,在排序列表之后???等等,嗯,你能给我一个提示吗。。我应该做什么?数组[c]、数组[d]或数组[d+1]?
int main() {
...
bubble_asc();
bubble_desc();
...
}

void bubble_asc() {
...
do step by step bubble sort with ascending order here.
for(~~~~~) {
    for(~~~~~~) {
        if(array[a] < array[b])
             ....
        }
 }
 print arrays here..
}
void bubble_desc() {
...
do step by step bubble sort with descending order here.
for(~~~~~) {
    for(~~~~~~) {
        if(array[a] > array[b])
             ....
        }
 }
 print arrays here..
}