how to sort an array c++
C++
// C++ program to demonstrate default behaviour of
// sort() in STL.
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = {1, 5, 8, 9, 6, 7, 3, 4, 2, 0};
int n = sizeof(arr)/sizeof(arr[0]);
sort(arr, arr+n);
cout << "\nArray after sorting using "
"default sort is : \n";
for (int i = 0; i < n; ++i)
cout << arr[i] << " ";
return 0;
}
#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;
}
// C++ program to demonstrate default behaviour of
// sort() in STL.
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = {1, 5, 8, 9, 6, 7, 3, 4, 2, 0};
int n = sizeof(arr)/sizeof(arr[0]);
sort(arr, arr+n);
cout << "\nArray after sorting using "
"default sort is : \n";
for (int i = 0; i < n; ++i)
cout << arr[i] << " ";
return 0;
} sort(arr, arr+n, greater<int>()); // sorts in descending order// C++ program to demonstrate descending order sort using
// greater<>().
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = {1, 5, 8, 9, 6, 7, 3, 4, 2, 0};
int n = sizeof(arr)/sizeof(arr[0]);
sort(arr, arr+n, greater<int>());
cout << "Array after sorting : \n";
for (int i = 0; i < n; ++i)
cout << arr[i] << " ";
return 0;
}
// A C++ program to demonstrate STL sort() using
// our own comparator
#include<bits/stdc++.h>
using namespace std;
// An interval has a start time and end time
struct Interval
{
int start, end;
};
// Compares two intervals according to staring times.
bool compareInterval(Interval i1, Interval i2)
{
return (i1.start < i2.start);
}
int main()
{
Interval arr[] = { {6,8}, {1,9}, {2,4}, {4,7} };
int n = sizeof(arr)/sizeof(arr[0]);
// sort the intervals in increasing order of
// start time
sort(arr, arr+n, compareInterval);
cout << "Intervals sorted by start time : \n";
for (int i=0; i<n; i++)
cout << "[" << arr[i].start << "," << arr[i].end
<< "] ";
return 0;
}
Also in C++:
- Title
- how do you add two random numbers in c++
- Category
- C++
- Title
- conditional operator in cpp
- Category
- C++
- Title
- pop from between string c++
- Category
- C++
- Title
- set lower bound c++
- Category
- C++
- Title
- c++ ros subscriber
- Category
- C++
- Title
- regexp_like oracle c++
- Category
- C++
- Title
- c++ files
- Category
- C++
- Title
- iterative preorder traversal
- Category
- C++
- Title
- c++ vector lower_bound index
- Category
- C++
- Title
- statement that causes a function to end in c++
- Category
- C++
- Title
- c++ set add element
- Category
- C++
- Title
- empty string c++ value
- Category
- C++
- Title
- how to swap string characters in c++
- Category
- C++
- Title
- how to iterate over unordered_map c++
- Category
- C++
- Title
- c++ for loop syntax
- Category
- C++
- Title
- char **
- Category
- C++
- Title
- arduino delay millis
- Category
- C++
- Title
- log base e synthax c++
- Category
- C++
- Title
- how to take input in C++ in coding
- Category
- C++
- Title
- how to compare lower case character to uppercase cpp
- Category
- C++
- Title
- how to use wasd c++
- Category
- C++
- Title
- quick sort predefined function in c++
- Category
- C++
- Title
- c++ char to string
- Category
- C++
- Title
- calling by reference c++
- Category
- C++
- Title
- simple timer arduino blynk library error
- Category
- C++
- Title
- create copy of range of string c++
- Category
- C++
- Title
- % operator in c++
- Category
- C++
- Title
- variabili in c++
- Category
- C++
- Title
- count a character in a string c++
- Category
- C++
- Title
- stl sort in c++
- Category
- C++
- Title
- c++ code to print hello world
- Category
- C++
- Title
- can we compare a long long int with int in c++ using max or min functions
- Category
- C++
- Title
- c++ typeid get type name
- Category
- C++
- Title
- c++ reset stream
- Category
- C++
- Title
- class is replace by structure
- Category
- C++
- Title
- how to output to console c++
- Category
- C++
- Title
- How to read a file in in C++
- Category
- C++
- Title
- print matrix c++
- Category
- C++
- Title
- how to declare a vector in c++
- Category
- C++
- Title
- how to sort vector in c++
- Category
- C++
- Title
- how to load from files C++
- Category
- C++
- Title
- minimum swaps to sort an array
- Category
- C++
- Title
- c++ how to return an empty vector
- Category
- C++
- Title
- new keyword in cpp
- Category
- C++
- Title
- c++ string to integer without stoi
- Category
- C++
- Title
- split 2d array into chunks in c++
- Category
- C++
- Title
- initialize int c++
- Category
- C++
- Title
- convert stirng to int c++
- Category
- C++
- Title
- c++ triple
- Category
- C++
- Title
- file objects in c++
- Category
- C++
- Title
- how to calculate inverse trigonometric values in c++
- Category
- C++
- Title
- first prime numbers less than
- Category
- C++
- Title
- set of vectors c++
- Category
- C++
- Title
- split string at index c++
- Category
- C++
- Title
- call by reference c++ example
- Category
- C++
- Title
- how use global variables instead of local in c++
- Category
- C++
- Title
- Convert binary tree to a doubly linked list
- Category
- C++
- Title
- how the theam are store in database
- Category
- C++
- Title
- matrix multiplication c++ eigen
- Category
- C++
- Title
- c++ initialize a vector
- Category
- C++
- Title
- chess perft 5
- Category
- C++
- Title
- root to leaf path print
- Category
- C++
- Title
- c++ cout int
- Category
- C++
- Title
- maximum subarray sum equal with K in c++
- Category
- C++
- Title
- modulo c++
- Category
- C++
- Title
- How to find the suarray with maximum sum using divide and conquer
- Category
- C++
- Title
- c++ loop through int array
- Category
- C++
- Title
- monotonic deque
- Category
- C++
- Title
- stack function in cpp
- Category
- C++
- Title
- *min_element in c++
- Category
- C++
- Title
- : error: ‘cont’ cannot be used as a function return (cont(cont-1))/2;
- Category
- C++
- Title
- Merge k sorted linked lists and return it as one sorted list.
- Category
- C++
- Title
- howt o initialize 3d vector in c++
- Category
- C++
- Title
- count number of zeros in array in O(logN)
- Category
- C++
- Title
- double to int c++
- Category
- C++
- Title
- iterate const vector
- Category
- C++
- Title
- mkdir boost filesystem
- Category
- C++
- Title
- c++ pi
- Category
- C++
- Title
- first fit algorithm
- Category
- C++
- Title
- C++ string format ctime
- Category
- C++
- Title
- Find a element in a map C++
- Category
- C++
- Title
- input a string in c++
- Category
- C++
- Title
- binary tree deletion
- Category
- C++
- Title
- how to get a letter from the users string in c++
- Category
- C++
- Title
- char vector to string c++
- Category
- C++
- Title
- pass vector by reference c++
- Category
- C++
- Title
- how to create object in c++
- Category
- C++
- Title
- how to make a 2d vector in c++
- Category
- C++
- Title
- built in function in c++ for binary to decimal
- Category
- C++
- Title
- friend function in c++
- Category
- C++
- Title
- cin.ignore
- Category
- C++
- Title
- c++ replace substrings
- Category
- C++
- Title
- convert integer to string c++
- Category
- C++
- Title
- c++ sort
- Category
- C++
- Title
- iterate 2d array c++
- Category
- C++
- Title
- pyqt connect
- Category
- C++
- Title
- two sum problem in c++
- Category
- C++
- Title
- get data from terminal c++
- Category
- C++
- Title
- remove element by index from vector c++
- Category
- C++
- Title
- remove from unordered_set c++
- Category
- C++