how to sort an array in c 2b 2b

Solutions on MaxInterview for how to sort an array in c 2b 2b by the best coders in the world

showing results for - "how to sort an array in c 2b 2b"
Maya
20 Jan 2019
1// C++ program to demonstrate default behaviour of 
2// sort() in STL. 
3#include <bits/stdc++.h> 
4using namespace std; 
5  
6int main() 
7{ 
8    int arr[] = {1, 5, 8, 9, 6, 7, 3, 4, 2, 0}; 
9    int n = sizeof(arr)/sizeof(arr[0]); 
10  
11    sort(arr, arr+n); 
12  
13    cout << "\nArray after sorting using "
14         "default sort is : \n"; 
15    for (int i = 0; i < n; ++i) 
16        cout << arr[i] << " "; 
17  
18    return 0; 
19} 
Dario
14 Nov 2018
1#include <bits/stdc++.h> 
2using namespace std; 
3
4#define size(arr) sizeof(arr)/sizeof(arr[0]);
5
6
7int main(){
8
9    int a[5] = {5, 2, 6,3 ,5};
10    int n = size(a);
11    sort((a), a + n);
12    for(int i = 0; i < n; i++){
13        cout << a[i];
14    }
15
16
17    return 0;
18
19}
20
Nick
29 Jun 2017
1#include <iostream>
2using namespace std;
3
4#define MAX 100
5
6int main()
7{
8	//array declaration
9	int arr[MAX];
10	int n,i,j;
11	int temp;
12	
13	//read total number of elements to read
14	cout<<"Enter total number of elements to read: ";
15	cin>>n;
16	
17	//check bound
18	if(n<0 || n>MAX)
19	{
20		cout<<"Input valid range!!!"<<endl;
21		return -1;
22	}
23	
24	//read n elements
25	for(i=0;i<n;i++)
26	{
27		cout<<"Enter element ["<<i+1<<"] ";
28		cin>>arr[i];
29	}
30	
31	//print input elements
32	cout<<"Unsorted Array elements:"<<endl;
33	for(i=0;i<n;i++)
34		cout<<arr[i]<<"\t";
35	cout<<endl;
36	
37	//sorting - ASCENDING ORDER
38	for(i=0;i<n;i++)
39	{		
40		for(j=i+1;j<n;j++)
41		{
42			if(arr[i]>arr[j])
43			{
44				temp  =arr[i];
45				arr[i]=arr[j];
46				arr[j]=temp;
47			}
48		}
49	}
50	
51	//print sorted array elements
52	cout<<"Sorted (Ascending Order) Array elements:"<<endl;
53	for(i=0;i<n;i++)
54		cout<<arr[i]<<"\t";
55	cout<<endl;	
56	
57	
58	return 0;
59	
60}
61
Stefania
20 Sep 2020
1#include <algorithm>
2#include <iostream>
3#include <array>
4using namespace std;
5
6int main() {
7    array<int, 5> arraysort{ 4,2,3,5,1 };
8    sort(arraysort.begin(), arraysort.end());
9    for (int i = 0; i < arraysort.size(); i++) {
10        cout << arraysort[i] << " ";
11    }
12	return 0; 
13}
Merissa
20 Jan 2021
1#include <iostream>
22 #include <array>
33 #include <string>
44 #include <algorithm>
55
66 using namespace std;
77
88 int main(){
99 array<string, 4> colours = {"blue", "black", "red", "green"};
1010 for (string colour : colours){
1111 cout << colour << ' ';
1212 }
1313 cout << endl;
1414 sort(colours.begin(), colours.end());
1515 for (string colour : colours){
1616 cout << colour << ' ';
1717 }
1818 return 0;
1919 }
2066
2120
2221 /*
2322 Output:
2423 blue black red green
2524 black blue green red
2625 */
Catalina
18 Nov 2019
1#include <iostream>
2using namespace std;
3int main () {
4
5  int n,i,j,temp;
6  cout << "how many Arrays you wanna sort? ";
7  cin >> n;
8  cout << endl << endl;
9  int a[n];
10  for(i = 0;i < n; i++){
11    cout << "please enter your " << i + 1 << " array : ";
12    cin >> a[i];
13  }
14  for(i = 0; i < n; i++){
15   for(j = 0; j < n - 1; j++){
16    if( a[j] > a[j+1]){
17     temp = a[j];
18      a[j] = a[j+1];
19        a[j+1] = temp;
20    }
21   }
22  }
23
24  cout << "\nArray after sorting using default sort is : \n";
25    for (i = 0; i < n; i++)
26      cout << a[i] << '\t';
27
28 return 0;
29}
queries leading to this page
how to sort numbers in c 2b 2b arrayhow to order an array of integers in c 2b 2barray built in sort in c 2b 2bho wto sort an array in c 2b 2bc 2b 2b sort codec 2b 2b array sortorder items in array c 2b 2border array c 2b 2bsort array in cppsort array and store result in intger c 2b 2bhow to sort a array in cppinbuilt sort function in c 2b 2bhow to sort a stack in c 2b 2bsort array c 2b 2b stlsort c 2b 2b array of arraysort integer array c 2b 2bsorting an array stlsort function c 2b 2bhow does sort work c 2b 2bhow to sort array elements in c 2b 2bsorted array c 2b 2bsort arrat in cppc 2b 2b sort array algorithmsort set c 2b 2bsorting array cpp stlsort c 2b 2b arraysort a array cppsort arrays inn c 2b 2bc 2b 2b program to sort an array using functionsc 2b 2b sort array in ascending orderdefine a sort function c 2b 2bsort in cpphow to override default sort method cppsort inctio in c 2b 2bsort an array in ascending order c 2b 2bsorting methods c 2b 2bhow to apply sort function in array in c 2b 2bc 2b 2b std 3a 3asort arraymanual sort array cppmodifing sort condittion in c 2b 2b stlsort int c 2b 2bcomp function sort c 2b 2bc 2b 2b element sorting algorithmsorting an array cppsort stl c 2b 2bsort arr using cpp sorgsorting array in ascending order c 2b 2bsort an arr in c 2b 2bc 2b 2b sort implementationsorting a array c 2b 2bsorting function implementation in c 2b 2bhow to customize sort stl in cpphow to sort elements in array in c 2b 2bsorting list c 2b 2bsort array function c 2b 2bstl sort conditionsort array c 2b 2b algorithmhow to use array sort in cppint array sort c 2b 2bstd 3a 3asort arraycpp sortsort array cppcan we use sort func using arrays in cppreverse sort in c 2b 2bsort an int array in cpphow to sort int array c 2b 2barray c 2b 2b sorting numbershow to sort array using stlhow to sort array in descending order in c 2b 2bsort on array in c 2b 2bstd 3a 3asort 28 29 in c 2b 2b is sort a function in a cstl c 2b 2b array sortc 2b 2b how to use std 3a 3asortsort an int array c 2b 2bsort function in c 2b 2b for arrayhow to sort array of integers in c 2b 2beasy sorting array c 2b 2bsort in c 2b 2bc 2b 2b sort syntaxsort arraylist in c 2b 2bsort array in c 2b 2b using libraryc 2b 2b sort array intscomp in sort c 2b 2bc 2b 2b sorting algorithmssort function without stdc in cppdecreasing order sort in c 2b 2bsorting arr 5b in c 2b 2bsort given array c 2b 2balgorithm sort of an array c 2b 2bsort array stl cppsort integer array c 2b 2b o 28n 29c 2b 2b can an array be sortedsorting of array in c 2b 2b codesort with comparator cpphow to sort an integer c 2b 2b functionis the any function to sort an array in c 2b 2bc 2b 2b sort listsimple c 2b 2b sorting programsort a c 2b 2b arraysort an aaray using cpp librarycpp sort functionsort function in array c 2b 2b explanationsorting in c 2b 2bsorting arrays c 2b 2bsorting function in c 2b 2bsort an simple array cppc 2b 2b arrray sorthow to sort integer in c 2b 2bsorting in c 2b 2b arrayhow to sort a integer in c 2b 2barray sort in c 2b 2bsort array c 2bfunction to sort integer array in c 2b 2b sort 28 29 in c 2b 2bsort in array c 2b 2bhow to sort an array from highest to lowest c 2b 2bsorting inorder c 2b 2bhow to sort array in c 2b 2b with forsort integer array c 2b 2b codesort algorithm c 2b 2bwhat is int sort in c 2b 2bcpp code for array sortingsorting array elements by stl c 2b 2bhow to use sort function in c 2b 2barrange int array in order c 2b 2bsort and array in c 2b 2bbest array sorting algorithm c 2b 2bsort array by size c 2b 2barr sort c 2b 2bsort an array in c 2b 2b stlsort numbers in descending order c 2b 2bc 2b 2b sortingsort items in an array c 2b 2bsorting integer in c 2b 2bsort function for an array c 2b 2bcpp array sortascending order program in c 2b 2bsorting an array using stlc 2b 2b algorithm sorthow to sort an interger array in c 2b 2bbuilt in array sort c 2b 2bc 2b 2b how to sort an arrayc 2b 2b sorting int arrayfunction sort c 2b 2barray sorting algorims c 2b 2bsort function in c 2b 2b with arraysport an array c 2b 2bhow to sort array c 2b 2bc 2b 2b array ordersort an integer array in c 2b 2bhow to sort a c 2b 2b arrayarray sort c 2b 2bhow to order an array in c 2b 2bsort c 2b 2bcomparison function sort c 2b 2bbuilt in sort function in c 2b 2bsort an array using stlhow to sort array by numerical order c 2b 2bsorts array c 2b 2bsorting the array in c 2b 2bsorting of array in c 2b 2b programhow to sort a array in c 2b 2bstl sort to sort a vectorsort array in c 2b 3dsorting array inc 2b 2bsorting in cppsorting algorithms cppfunction for sorting arrays c 2b 2bfunction to sort an array in ascending order c 2b 2bc 2b 2b function array sortc 2b 2b sort using on an arraysort array in ascending order c 2b 2bsort array of arrays in c 2b 2bsort stl in c 2b 2bhow to sort an array using inbuilt function in c 2b 2bhow to sort elements in array c 2b 2bcpp sortingint sorting c 2b 2bcomparator function for sort in c 2b 2bsorting order in c 2b 2bsort method cppsort function in c 2b 2bsort c 2b 2b inthow to sort an array in ascending order c 2b 2breverse sort function in c 2b 2bwrite a program to sort an array in c 2b 2bhow to use comp function in sort in stl c 2b 2bc 2b 2b sort increasing orderhow to sort all array in c 2b 2bsort c 2b 2b decreasingc 2b 2b sortsort array using stl in cppsort in c 2b 2b parameterssort with comparator c 2b 2bbest sorting stl c 2b 2bsort array c 2b 2bc 2b 2b sort algorithmsort highest to lowest c 2b 2buse sort function in c 2b 2barray sorting shortcut in c 2b 2bsort function in c 2b 2b descendingshortcut to print sorted array in ccpp array sorterc 2b 2b sort what algorithmc 2b 2b sort array sort 28 29 in c 2b 2bfunction to sort an array in c 2b 2barray sorting function in c 2b 2bsort an array with int cppsorting program in cpparrays sort in c 2b 2bhow to sort an array by value in c 2b 3dc 2b 2b how to sort arraydecending sort in c 2b 2bhow can sort number in a array c 2b 2bsort c 2b 2b stlsort func in cpp for arrayarrange array in ascending order in c 2b 2b using sortingc 2b 2b sort with functionsorting an array c 2b 2bhow to sort an array using sort function in c 2b 2bsort a array c 2b 2bsort stl examplesorting cpp c 2b 2b predefined sort function arraydeclare sort in c 2b 2bstl in c 2b 2b sortsort func in cppis there sort function in array c 2b 2bsort an array c 2b 2b ascendinghow c std sort worksc 2b 2b code to sort an array in ascending order using sort algorithmsorting array in cppsort an array in c 2b 2b by stlsort by ascending array c 2b 2bc 2b 2b sort array of intshow to sort an array in c 2b 2bsort array in ascending order c 2b 2bfunction to sort array in cppstd array c 2b 2b sorthow to sort an array in cpphow to sort array in c 2b 2b stlstl library for ascending order c 2b 2bbubble sort array c 2b 2bsort 28 29 cpphow to sort an array in c 2b 2b with a functionc 2b 2b array sort methodarray 3cint 3e sort in cppsorting numbers c 2b 2barray sort cppsort header in cppc 2b 2b sorting array of numberssort array using sort function c 2b 2bsorting in array in c stlsort c 2b 2b syntaxc 2b 2b sort compare function examplec 2b 2b acending sort algoc 2b 2b sort int array algorithmarray sorting in c 2b 2b inputs taken by usersort using key stl c 2b 2bsort 28 29 c 2b 2bhow to sort elements in nascending order in array in c 2b 2bsort array in functipon c 2b 2binbuild sort functionin which sort method does cpp sort function worksarrange numbers in array c 2b 2bhow to sort descending in c 2b 2bhow to sort an array in c 2b 2bsorting algorithms programs in c 2b 2bstl for sorting arraysort array inusing sort in c 2b 2bsorting array code in c 2b 2bsort 28a 2ca 2bn 2cgreater 3cint 3e 28 29 29 3bsort 28 29 for array in c 2b 2bhow sort function works in c 2b 2bshorting an array in cppwhat is sort function in c 2b 2bsort array c 2b 2b examplehow to use sorting function in array c 2b 2bis there any direct sort function in strings in c 2b 2bsorting in array in c 2b 2binbuilt function to sort array in c 2b 2barray sort function in c 2b 2bc 2b 2b sorted arrayc 2b 2b code for sorting an arraysort with respect to a parameter in c 2b 2barrange students c 2b 2bsorting of array c 2b 2bsort function in c 2b 2b arraysort stl cppsort array in cpp stlsort array stdc 2b 2b sort array ascendingcan you use sort on array in c 2b 2bhow to sort array in cppusing sort in c 2b 2bsort an int array in c 2b 2bsort array on c 2b 2bsort array cpp integershow can sort integer of array in c 2b 2bsort ing in c 2b 2bfunction to sort array in c 2b 2bsort an array in cppalgorithm sort c 2b 2barray c 2b 2b sortsorting of array in cpphow to sort an array in c 2b 2b examplesort c 2b 2b built in function for arrayhow can i sort an array in c 2b 2b 3fsort function code in c 2b 2bc 2b 2b code to sort an array in ascending orderhow to use sort in c 2b 2bhow to sort array in c 2b 2b use algorithmsort c 2b 2b functionsort int 2a c 2b 2bc 2b 2b int array sortsort methods for arrays in c 2b 2bhhow to sort an array in c 2b 2bhow to sort array in cpp using sortarrays sort 28nums 29 3b cppsort stl c 2b 2b asc dessort a given array using c 2b 2bc 2b 2b sort 28 29int arr sort c 2b 2bhow to sort an array in csort int cppsorting the elements of an array in c 2b 2bsort c 2b 2b comparatorfunction to sort an array in cppsorting array c 2b 2bwhat is the function to sort in c 2b 2bsort function in cppreverse sort cppsort the array in c 2b 2bc 2b 2b sort functionsorting stlhow to sort int array in c 2b 2bsorting implementation in c 2b 2bsort an array using sort in c 2b 2bsort fn in c 2b 2bhow to sort an array using stlhow to sort integer array in c 2b 2bc 2b 2b sort function arrayhow does sort function work in c 2b 2bc 2b 2b buildin sort functionc 2b 2b sorting arrayhow to sort an array in c 2b 2b 3fhow to sort in descending order in cppsort a array elment in c 2b 2bc 2b 2b stl array sortcpp sort arraysorting an array in c 2b 2b programc 2b 2b function to sort an arrayhow to sort any anrray in c 2b 2bsorting with c 2b 2bhow to write a program that sorts an array in c 2b 2bsort ann array c 2b 2barrange array in ascending order c 2b 2bsort methods for arraysin c 2b 2bsort numbers in c 2b 2bc 2b 2b program to sort array in ascending orderarray simple sort c 2b 2bsort 28 29 function in cppsorting algorithms in cppsort function in c 2b 2b implementationsort c 2b 2b integersorting a array in c 2b 2bsort a integer in c 2b 2bsorting an integer array in c 2b 2bsorting an array in c 2b 2bsorting array in cpp using include algorithmsort inbuilt function in c 2b 2bhow to directly sort a array in c 2b 2bc 2b 2b sorting an arrayhow to sort array in c 2b 2bsort array ascending order numbers c 2b 2busing inbuilt sort functionsorting array in c 2b 2bcompare funciton in sort c 2b 2bsort array in c 2b 2b ascending orderhow to use sort func in c 2b 2bhow to sort a array c 2b 3dsort function in descending ordersort an array c 2b 2b given an sort array c 2b 2b ascendingc 2b 2b sort int arrayc 2b 2b array in ascending ordersorting algorithms c 2b 2bsorting of numbers c 2b 2bsort function cppsorting of array in c 2b 2bsort array stlhow to sort an array in c 2b 2bsort c 2b 2b manualhow to write array sort function c 2b 2bcpp stl sort arrasort an array in c 2b 2bsorting code in c 2b 2barray sort library c 2b 2bstl to sort an array in csort an array stlhow to sort by second value where first must be small in cppstd 3a 3asorting arr in c 2b 2bsort array using stlsort array 2b 2bsort featurs in stlsort an array in ascending order cpphow can sort integer in array in c 2b 2bcpp sort function 5csort arr in cppgreater to smaller sort c 2b 2bsort function in c 2b 2b algorithmhow to sort a structure array in c 2b 2bsorting a long long int array in c 2b 2bsorting array in c 2b 2b codesorting array using sort cpphpw can sort the integer of array in c 2b 2bsorting cpp codesort function for array in c 2b 2bhow to specify which sort to apply in c 2b 2barrays sort 28 29 c 2b 2bsort arrays in c 2b 2bfunc sort array c 2b 2bsorting an array in cppsort an array c 2b 2bsort array c 2b 2b librarysort 28 29 in 2b 2bsort array in c 2b 2b algorithmsort array c 2b 2b manualarray sort function c 2b 2bsort items in array c 2b 2bhow to arrange elements of array in ascending order in c 2b 2bc 2b 2b sort address of arraysort int array c 2b 2bsort array in ascending numbers c 2b 2bsort array library c 2b 2bsort an array of integers c 2b 2bsort array function in c 2b 2bsort stlfor sort a array element in c 2b 2bhow we can sort array in c 2b 2bsorting c 2b 2b rrayhow to sort an array c 2b 2bsortarray function in c 2b 2bc 2b 2b order array by sizesort an array c 2b 2b stlhow to sort array in increasing order in c 2b 2bsort array numbers c 2b 2bhow can sort element of array in c 2b 2bc 2b 2b sorting arrayssort an array cpphow to sort array in c 2b 2b using stllibrary function to sort an array in c 2b 2bc 2b 2b int sortsort in decreasing order in c 2b 2bsort method in c 2b 2bsort comparator c 2b 2barray sorting in c 2b 2bcompare functiokn in stl sortc 2b 2b function for sortinwhen we use the sort function in c 2b 2b which sort is usedcompare function of sort in c 2b 2bsort int array in c 2b 2barray sort 28 29 in c 2b 2bsort arrays c 2b 2bsort array in c 2b 2bhow to sort array cppc 2b 2b sort an arrayhow to place an element in sorted array in c 2b 2b using stlsorting integer array in c 2b 2bsorting algorithm c 2b 2bsorting a array by making a new array in cppstandard sort c 2b 2b arrayssort arr in c 2b 2b stlsort array c 2b 2b algorithm librarycmp function in cpp for sortuse sort 28 29 in c 2b 2bsort a array element in c 2b 2bsort cppsort in descending order c 2b 2bc 2b 2b order arrayc 2b 2b writing function to sort algorithmsort array in c 2fc 2b 2bcode to sort an array in c 2b 2bhow to use the sort function in c 2b 2bsorting c 2b 2bjava sort arraysort array ascending order c 2b 2bsort function c 2b 2barr sort c 2b 2binbuilt c 2b 2b function to sort an arraysort function in c 2b 2b 5csort function in array in c 2b 2bcpp sort int arraysorting arra y in c 2b 2bsort array c 2b 2b functioncpp create sorted arraysorting arrays in c 2b 2bsort function of array in c 2b 2barray sort in cpphow to sort an array in c 2b 2b