How to sort an array in a single loop?

后端 未结 22 2909
面向向阳花
面向向阳花 2020-12-19 09:14

So I was going through different sorting algorithms. But almost all the sorting algorithms require 2 loops to sort the array. The time complexity of Bubble sort & Insert

22条回答
  •  温柔的废话
    2020-12-19 09:26

        #include
    
        void sort(int a[],int n,int k,int w)
        {   
          int i,j,z,key;
          n=n-1;
          j = k+1;
          key = a[j];
          i = j-1;
          while(i>0 && a[i]>key)
          {
            a[i+1] = a[i];
            i = i-1;
          }
          a[i+1] = key;
          k = k + 1;
          if(n!=0)
          {
           sort(a,n,k,w);
          }
        }
    
      int main()
     {
        int i,n,w,k=1,z=5,g;
        printf("enter the size of an array\n");
        scanf("%d",&n);
        g=n;
        int a[n];
        for(i=1;i<=n;i++)
        {
            scanf("%d", &a[i]);
        }
        w = n;
        sort(a,n-1,k,w);
    
        for(i = 1; i <= n; i++)
        {
            printf("%d", a[i]);
        }
     }
    

    Here is the solution. This might help you.

提交回复
热议问题