简体   繁体   中英

Sorting arrays and values in c++

#include <iostream>
using namespace std;

int main() {
    int a=70, b=40, c=5, d=1, e=-20, f=90, g=2, mid;
    int array[7]={a, b, c, d, e, f, g};
    for (int i = 0; i<=5; i++) {
        while (i=0) {
            if (a>b && a>c && a>d && a>e && a>f && a>g) {
                mid=g;
                g=a;
                a=mid;
            }
                else if (b>a && b>c && b>d && b>e && b>f && b>g) {
                    mid=g;
                    g=b;
                    b=mid;
                }
                else if (c>a && c>b && c>d && c>e && c>f && c>g) {
                    mid=g;
                    g=c;
                    c=mid;
                }
                else if (d>a && d>c && d>b && d>e && d>f && d>g) {
                    mid=g;
                    g=d;
                    d=mid;
                }
                else if (e>a && e>c && e>d && e>b && e>f && e>g) {
                    mid=g;
                    g=e;
                    e=mid;
                }
                else if (f>a && f>c && f>d && f>e && f>b && f>g) {
                    mid=g;
                    g=f;
                    f=mid;
                }
                else if (g>a && g>c && g>d && g>e && g>f && g>b) {
                    mid=g;
                    g=g;
                    g=mid;
                }
        }
        while (i=1) {
            if (a>b && a>c && a>d && a>e && a>f) {
                mid=f;
                f=a;
                a=mid;
            }
                else if (b>a && b>c && b>d && b>e && b>f) {
                    mid=f;
                    f=b;
                    b=mid;
                }
                else if (c>a && c>b && c>d && c>e && c>f) {
                    mid=f;
                    f=c;
                    c=mid;
                }
                else if (d>a && d>c && d>b && d>e && d>f) {
                    mid=f;
                    f=d;
                    d=mid;
                }
                else if (e>a && e>c && e>d && e>b && e>f) {
                    mid=f;
                    f=e;
                    e=mid;
                }
                else if (f>a && f>c && f>d && f>e && f>b) {
                    mid=f;
                    f=b;
                    b=mid;
                }
        }
        while (i=2) {
            if (a>b && a>c && a>d && a>e) {
                mid=e;
                e=a;
                a=mid;
            }
                else if (b>a && b>c && b>d && b>e) {
                    mid=e;
                    e=b;
                    b=mid;
                }
                else if (c>a && c>b && c>d && c>e) {
                    mid=e;
                    e=c;
                    c=mid;
                }
                else if (d>a && d>c && d>b && d>e) {
                    mid=e;
                    e=d;
                    d=mid;
                }
                else if (e>a && e>c && e>d && e>b) {
                    mid=e;
                    e=e;
                    e=mid;
                }

        }
        while (i=3) {
            if (a>b && a>c && a>d) {
                mid=d;
                d=a;
                a=mid;
            }
                else if (b>a && b>c && b>d) {
                    mid=d;
                    d=b;
                    b=mid;
                }
                else if (c>a && c>b && c>d) {
                    mid=d;
                    d=c;
                    c=mid;
                }
                else if (d>a && d>c && d>b) {
                    mid=d;
                    d=d;
                    d=mid;
                }

        }
        while (i=4) {
            if (a>b && a>c) {
                mid=c;
                c=a;
                a=mid;
            }
                else if (b>a && b>c) {
                    mid=c;
                    c=b;
                    b=mid;
                }
                else if (c>a && c>b) {
                    mid=c;
                    c=c;
                    c=mid;
                }


        }
        while (i=5) {
            if (a>b) {
                mid=b;
                b=a;
                a=mid;
            }
                else if (b>a) {
                    mid=b;
                    b=b;
                    b=mid;
                }
        }
    }

    for (int k=0; k<7; k++){
        cout<<array[k]<<" ";
    }

}

This program does not give me an output... What do I do instead? I have tried everything, but it just won't get through to the execution part? What do I do?

  1. You confused assignment ( = ) with comaprison ( == ). while(i = x) will assign x to i , then evaluate i , so it's equal to while(x) , which does never run if x is 0 and endless otherwise.

  2. Your loops are still endless. Take for example the (fixed) loop while (i == 3) . i is never modified inside that loop and there is no break or return in it, so even the fixed version will run endlessy. But that's a design issue you need to solve. Maybe there should be a else { break; } else { break; } after all those if 's?

  3. You will still get the wrong result, because you swap the values of the variables, a , b , c , d , e , f and g , but never modify array . However in the end, you print the values of array. The array does not store items by reference, but copies their values. Why do you even use so many variables and don't operate on the array directly?

  4. The last else if(..) block in each while is useless. Take a close look at - for example - this code:

     mid=d; d=d; d=mid;

    It assigns the value of d to d . Twice .

  5. As already mentioned in the comments, sorting is implemented in the standard library. Use std::sort instead of writing your own sort function:

     std::sort(std::begin(array), std::end(array));

You can use std or boost library for sorting. Or else you can code yourself. Please go through the below code.

#include<iostream> 
using namespace std;

void selectionSort(int a[], int n) {
   int i, j, min, temp;
   for (i = 0; i < n - 1; i++) {
      min = i;
      for (j = i + 1; j < n; j++)
         if (a[j] < a[min])
            min = j;
      temp = a[i];
      a[i] = a[min];
      a[min] = temp;
   }
}
int main() {
   int a[] = { 22, 91, 35, 78, 10, 8, 75, 99, 1, 67 };
   int n = sizeof(a)/ sizeof(a[0]);
   int i;
   cout<<"Given array is:"<<endl;
   for (i = 0; i < n; i++)
   cout<< a[i] <<" ";
   cout<<endl;
   selectionSort(a, n);      // calling my function selection sort. This will sort as ascending order

   for (i = 0; i < n; i++)  
   cout<< a[i] <<" ";       // Printing sorted array
   return 0;
}

Above code is for ascending order.

For Descending order refer: https://www.includehelp.com/cpp-programs/sort-an-array-in-descending-order.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM