Answers for "sort() function in cpp"

C++
0

c++ sort

#include<bits/stdc++.h>

vector<int> v = { 6,1,4,5,2,3,0};
sort(v.begin() , v.end()); // {0,1,2,3,4,5,6} sorts ascending
sort(v.begin(), v.end(), greater<int>()); // {6,5,4,3,2,1,0} sorts descending
Posted by: Guest on May-05-2021
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