Answers for "how to make a sort function in c++ array"

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
0

how to sort array in c++

#include <iostream>
using namespace std;
int main () {

  int n,i,j,temp;
  cout << "how many Arrays you wanna sort? ";
  cin >> n;
  cout << endl << endl;
  int a[n];
  for(i = 0;i < n; i++){
    cout << "please enter your " << i + 1 << " array : ";
    cin >> a[i];
  }
  for(i = 0; i < n; i++){
   for(j = 0; j < n - 1; j++){
    if( a[j] > a[j+1]){
     temp = a[j];
      a[j] = a[j+1];
        a[j+1] = temp;
    }
   }
  }

  cout << "nArray after sorting using default sort is : n";
    for (i = 0; i < n; i++)
      cout << a[i] << 't';

 return 0;
}
Posted by: Guest on September-10-2021

Browse Popular Code Answers by Language