其他分享
首页 > 其他分享> > get the largest values in array

get the largest values in array

作者:互联网

The results:

The original array is:
The array a :
   4   6   2   6  53   2   6  12
The array after the selection is:
The array a :
   4   2   6   6   2   6  12  53

The codes:

// obtain the results from  the selection sort function by recrusive methods
#include<iostream>
#include<iomanip>
void swap(int &i, int &j);
void selectionSort(int a[], int n);
void displaySelection(int a[], int n);
using namespace std;
int main()
{
    int a[] = {4,6,2,6,53,2,6,12};
    int n = sizeof(a) / sizeof(a[0]);
    selectionSort(a, n);
    displaySelection(a, n);
    return 0;
}
void selectionSort(int a[], int n)
{
    if(n ==1){
        return ;
    }
    selectionSort(a,n-1);
    if(a[n-1] < a[n-2]){
        swap(a[n-1],a[n-2]);
    }
}
void displaySelection(int a[], int n)
{
    cout <<"The array a : " << endl; 
    for(int i = 0; i < n; i++){
        cout << setw(4) << a[i];
    }
}
void swap(int &i, int &j)
{
    int temp;
    temp = i;
    i = j;
    j = temp;

}

标签:12,selectionSort,int,void,displaySelection,values,largest,array
来源: https://blog.csdn.net/weixin_38396940/article/details/120919128