Answers for "sort function in array c++ explanation"

C++
5

how to sort an array in c++

#include <bits/stdc++.h> 
using namespace std; 

#define size(arr) sizeof(arr)/sizeof(arr[0]);


int main(){

    int a[5] = {5, 2, 6,3 ,5};
    int n = size(a);
    sort((a), a + n);
    for(int i = 0; i < n; i++){
        cout << a[i];
    }


    return 0;

}
Posted by: Guest on March-18-2020
1

sorting algorithm c++

#include<iostream>
using namespace std;

int main(int argc, char const *argv[])
{
    int numb[7];
    int i, j;

    for(i=0;i<=6;i++)
    {
        cout << "Enter a number" << endl;
        cin >> numb[i];
    }

    for (i=0;i<=5;i++)
    {
        for (j=i+1;j<=5;j++)
        {
            int temp;

            if (numb[i] > numb[j])
            {
                temp = numb[i];
                numb[i] = numb[j];
                numb[j] = temp;               
            }
          }
        }
        for (i=0;i<=6;i++)
        {
            cout << endl << numb[i] << endl;
        }
}
Posted by: Guest on May-02-2021

Code answers related to "sort function in array c++ explanation"

Browse Popular Code Answers by Language