C++ sorting array

// 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; 
} 

0
0
Halst 95 points

                                     int arr[]= {2,3,5,6,1,2,3,6,10,100,200,0,-10};
    int n = sizeof(arr)/sizeof(int);  
    sort(arr,arr+n);

    for(int i: arr)
    {
        cout &lt;&lt; i &lt;&lt; &quot; &quot;;
    }

0
0
3.25
4
Kudlatiger 80 points

                                    #include &lt;iostream&gt;
using namespace std;

#define MAX 100

int main()
{
	//array declaration
	int arr[MAX];
	int n,i,j;
	int temp;
	
	//read total number of elements to read
	cout&lt;&lt;&quot;Enter total number of elements to read: &quot;;
	cin&gt;&gt;n;
	
	//check bound
	if(n&lt;0 || n&gt;MAX)
	{
		cout&lt;&lt;&quot;Input valid range!!!&quot;&lt;&lt;endl;
		return -1;
	}
	
	//read n elements
	for(i=0;i&lt;n;i++)
	{
		cout&lt;&lt;&quot;Enter element [&quot;&lt;&lt;i+1&lt;&lt;&quot;] &quot;;
		cin&gt;&gt;arr[i];
	}
	
	//print input elements
	cout&lt;&lt;&quot;Unsorted Array elements:&quot;&lt;&lt;endl;
	for(i=0;i&lt;n;i++)
		cout&lt;&lt;arr[i]&lt;&lt;&quot;\t&quot;;
	cout&lt;&lt;endl;
	
	//sorting - ASCENDING ORDER
	for(i=0;i&lt;n;i++)
	{		
		for(j=i+1;j&lt;n;j++)
		{
			if(arr[i]&gt;arr[j])
			{
				temp  =arr[i];
				arr[i]=arr[j];
				arr[j]=temp;
			}
		}
	}
	
	//print sorted array elements
	cout&lt;&lt;&quot;Sorted (Ascending Order) Array elements:&quot;&lt;&lt;endl;
	for(i=0;i&lt;n;i++)
		cout&lt;&lt;arr[i]&lt;&lt;&quot;\t&quot;;
	cout&lt;&lt;endl;	
	
	
	return 0;
	
}

3.25 (4 Votes)
0
4
1
Jdix81 100 points

                                    sort(arr, arr+length); //increase
sort(arr, arr+length, greater&lt;int&gt;()); //decrease 

4 (1 Votes)
0
3
1
Jidl 105 points

                                    #include &lt;iostream&gt;
2 #include &lt;array&gt;
3 #include &lt;string&gt;
4 #include &lt;algorithm&gt;
5
6 using namespace std;
7
8 int main(){
9 array&lt;string, 4&gt; colours = {&quot;blue&quot;, &quot;black&quot;, &quot;red&quot;, &quot;green&quot;};
10 for (string colour : colours){
11 cout &lt;&lt; colour &lt;&lt; ' ';
12 }
13 cout &lt;&lt; endl;
14 sort(colours.begin(), colours.end());
15 for (string colour : colours){
16 cout &lt;&lt; colour &lt;&lt; ' ';
17 }
18 return 0;
19 }
66
20
21 /*
22 Output:
23 blue black red green
24 black blue green red
25 */

3 (1 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
sort array c++ library std sort in c++ sort array and store result in intger c++ sort array library c++ sort array in cpp stl cpp sort function\ ho wto sort an array in c++ sort() in c++ library C++ array sort method int sorting c++ sort an integer array in c++ array sort in c++ how to include sort in cpp sort array in ascending numbers c++ c++ sort what algorithm c++ code for sorting an array sort array in functipon c++ sort array in c++ ascending order sort func in cpp sort function of array in c++ sorting arrays c++ sorting algorithms cpp c++ sort array in ascending order cpp sort array sort function in c++ structure sort an int array in c++ sort decending cpp how to use sort of cpp how to use sort func in c++ c plus plus sort sort array c++ algorithm library sort function in c++ with array c++ sort syntax sort array on c++ how we can sort array in c++ how to use sort function in c++ sort array inc++ arrange int array in order c++ sort c++ code STL sort c++ define a sort function c++ sort code c++ function sort c++ SortArray function in c++ sort algorithm code c++ sort function in C plus plus algorithm sort c++ syntax sort function in array in c++ sorting integer in c++ can we use sort func using arrays in cpp sorts array c++ sort array in ascending order - c++ sort in c++uses which algorithm sort arr in C++ stl cpp array sort array sorting shortcut in c++ cpp sort function code sort methods for arraysin c++ sort in c++ using array sort in c++ stl sort function in c++ cpp reference sort function in c++ operator how to sort an array in ascending order c++ sort integer array c++ sort function in c++ array c++ array order what algorithm sort use c++ c++ program to sort an array using functions sort the array in c++ how to use sorting function in array c++ sorting algorithm c++ What is the sorting in c++ sort int cpp cpp sort int array sort() in c++ stl sort structure c++ implement c++ sort algorithm c++ sorting algorithm sort the array c++ std::sort() in C++ sorting in cpp how to sort array in cpp using sort sort arr using cpp sorg c++ sort function uses which algorithm hhow to sort an array in c++ sort stl in cpp sort arrat in cpp sort function library in c++ sort cpp sort array c++ algorithm is there sort function in array c++ sorting algorithms in cpp sort on array in c++ manual sort array cpp sort integer array c++ o (n) std array c++ sort std::sort cpp sort int c++ how to sort an array using sort function in c++ sort ( c++ c++ sorting int array how to include sort c++ how to sort a array in c++ sort a array c++ c++ sorting array of numbers cpp sort() sorting algorithms c++ sorting array code in c++ what is sort function in c++ sort func in cpp for array array.sort in c++ c++ sort with function c++ sorting algorithms C++ sort int array algorithm c++ sort implementation c++ sort documentation using std sort c++ how to order an array in c++ int arr sort c++ order by cpp incresing order sort cpp sorting function in c++ sort() library in c++ sort int array in c++ sort function in c++ library c++ element sorting algorithm sort command c++ std sort function c++ cpp sort function is which sort algorithm how does sort function work in c++ sorting function in cpp sort arr in cpp how to sort array of integers in c++ c++ std::sort algorithm using sorting in cpp sorting an integer array in c++ sort() in c++ int array sort c++ sort function c++ what order of sort how to sort a integer in c++ sort array ascending order numbers c++ sort array ascending order c++ sort in c++ coustom function sort function in cpp func sort array c++ sort() in ++ sort() inc ++ sort on structure in c++ sorting integer array in c++ c++ std::sort array how to order an array of integers in c++ sort function syntax in c++ sorting array using sort cpp sort in c++ which cpp std::sort include sort c++ sorting of Array in CPP sort c++ algorithm sort int * c++ c++ int sort array.sort() in c++ sort arrays c++ sort an array using sort in c++ c++ std sort stl cpp sort sort c++ int sort c++ integer sort array cpp sort array in c++ using library sort array c++ ascending array sort function c++ sort() function in c++ sorting in c++ array sorting with c++ sort function cpp how to sort using stl sorting a long long int array in c++ array c++ sort sorting logic c++ sorting an array using stl c++ sorting an array how to sort array in c++ with for sort header in cpp std::sort for array sort algorithm c++ array sort highest to lowest c++ compare function in sort c++ c++ order array by size algorithm sortc++ c++ sort class sort an array using stl sort by function c++ c++ sort address of array how to sort a c++ array cpp std sort reverse how to sort int array in c++ sort funtion stl sort stl first uses gfg is sort a function in a c sort numbers in c++ c++ sort algorithm library sort class c++ c++ built in sorting algorithms sorted array c++ c++ built in sort sort using c++ function to sort an array in cpp compare functiokn in stl sort sort(a,a+n,greater&lt;int&gt;()); sort() in c sort in c++ using stl how to use comp function in sort in stl c++ selection sort c++ sort array std sort function header file for sort in c++ sorting order in c++ sorting algo in c++ sorting array c++ bubble sort c++ std sort sort std c++ sort c++ function sort in c++ algorithm.h sorting an array in c++ with inbuilt sort function sort()in c++ sort an int array in cpp array sorting algorithm c++ std::sort in c comparator function for sort for descending order c++ sort list cpp stl sort arra cpp sort algorithm how to sort an integer in ascending order using C++ function sort array +/c+ sort an int array c++ library for sort function in c++ cpp array sorter c++ array sort algorithm c++ sort algorithm function sort in c++ function order crossover c++ how to sort array in c++ stl bubble sort array c++ predefined sorting array in c++ std::sorting arr in c++ sort c++ array of array sorting algorithms programs in c++ sort an array in ascending order c++ sort set c++ is there any sort function in array in c++ inbuilt c++ function to sort an array sorting list c++ c++ sorted array std srot how c std sort works c++ include sorting c++ include sort custom comparator sort c++ std vector sort stl to sort an array in C sort array using stl in cpp c+ sorting c++ sort array library comparison function sort c++ c++ sort array ascending Using inbuilt sort function c++ sort compare function vector functions c++ sort algorithm sort c++ iterator sort stl c++ asc des hpw can sort the integer of array in c++ how can sort element of array in c++ void Sort c++ for sort a array element in c++ sort a array element in c++ sort a array elment in c++ how can sort integer of array in c++ how can sort number in a array c++ sort using some function in c++ how to override default sort method cpp vector sort algorithm c++ c++ sorted order array c++ sorting a vector c++ c++ algorithm sorting sort arrays inn c++ sorting c++ stl for sort sort array c++ c++ algorithm library sort stl sort begin c++ how to sort an array c++ buildin sort function sort st c++ C algorithm sort sort an array c++ given an stl sorting algorithms sorting program in cpp sort int array c++ array c++ sorting numbers sorting stl sorting a array c++ sort stl example sort( greater&lt;int&gt;) c++ sort c++ array array sort in function in c++ sort items in an array c++ c++ sort compare function' cpp import sort how to sort all array in c++ sort an arr in c++ sorte c+= example sort featurs in stl sort(all(vals))c++ sort descending c++ stl sorting arra y in c++ sort array c++ manual sort c++ manual how to use inbuilt sort function in c++ sort a segment of an array in c++ using stl sort array cpp integers c++ sort vector custom function sort in c++ parameters sort documentation cpp in-order function c++ std sort c++ sorting c++ sorts c++ sort arraylist in c++ sorting library in c++ c++ sort algorithm sort array function in c++ how to use sort in Cpp c++ sort exaple sort array using sort function c++ how to sort an array using stl sort c++ decreasing sort function in c++ descending sort comparator c++ c++ sort compare function example C++ order array sorting array in c++ code compare function of sort in c++ function to sort an array in c++ c++ custom sort sort function in descending order c++ sort stl .sort in c++ c++ sort an array greater to smaller sort c++ how to change the sort in c++ sort header c++ defining your own sort c++ sort in c++; array sort c++ function sort a integer in c++ how to sort a array in cpp sample sort c++ how to sort an array in c ++ sort in cpp sort an array c++ stl sort based on comp c++ how to sort in descending order in cpp sort with respect to a parameter in c++ sorting a in c++ c++ inbuilt sorting function algorithms comparator sort c++ sorting in array in c stl sort an array cpp import in c++ for sort is there any direct sort function in strings in c++ comp function c++ sort() c++ doc sorting array in c++ c++ array sort c++ sorting array sort function for array in c++ soring in cpp sort in decreasing order in c++ reverse sort cpp sort c++ comparator c++ predefined sort function array sort array c++ function sort a array cpp std sort c++ string standard sort c++ std::sort c++ sort c++\ built in sort function in c++ sort given array c++ best sorting stl c++ .sort() in c++ sort c++ custom comparator sort int vector c++ sorting inorder C++ sorting inplace STL C++ inbuilt function to sort a vector in c++ how to sort an array from highest to lowest c++ decending sort in c++ sorting an array cpp c++ module to sort an array reverse sort function in c++ c++ sort() array sorting function in c++ c++ sort increasing order how to specify which sort to apply in C++ sort with comparator c++ sort vector in c++ how to sort in c++ decreasing order sort in c++ shortcut to print sorted array in c c++ sorting a predefined array sort a given array using c++ sort c++ library sort array stl sort stl c++ c++ stl array sort sorting an array in c++ program modifing sort condittion in c++ stl stl library for ascending order C++ function of sort in c++ how to sort array in cpp sort array function c++ sorting an array stl sort array in c/c++ cpp create sorted array include for sort c++ comparator function for sort in c++ sort using key stl c++ sort with comparator cpp using sort function in c++ for competitve programming sorting an array in c++ sort array in ascending order c++ sort with function c++ how to sort elements in array in c++ sort inctio in c++ sort fn in c++ c++ sorting library c++ all stl sorts function to sort array in c++ cpp comparator how to sort by second value where first must be small in cpp sorting array in cpp c++ sort array maintain order in custom sort c++ sort array in c++ using sort how to write a program that sorts an array in c++ c++ sorrt std::sort array sort an array in cpp sort function without stdc in cpp USING SORT IN C++ sort stl in c++ sorting cpp code use std sort in c++ compare funciton in sort c++ sort array numbers c++ STL sort to sort a vector sort cpp stl vector.sort comp in sort c++ C++ library sorting sorting arrays in c++ wit libraries std:;sort c++ first sort algorithm c++ cmp sort stl sort function sort array in c++ algorithm sort function in c++ stl sorting an array in cpp c++ vector.sort sort in descending order c++ comp function sort c++ what is the sort algorithm in the stl sort std::sort operator&lt; c++ sort include array sort cpp arrange numbers in array c++ sort comparator cpp sort stl cpp reverse sort in c++ sort stl c++ is which sort simple c++ sorting program .begin .end sorting c++ how to sort an array by value in c+= sorting array cpp stl c++ how to use std::sort Arrange students c++ sort vector c++ sorting c++ stl java sort array cpp sort function stl sort in c++ c++ sort vector sorting arrays in c++ sort function c++ documentation array sort c++ how to sort any anrray in c++ c++ how to sort array sorting the array in c++ how to sort array c++ sort array in cpp c++ sort vector header file c++ std::sort sort in c++ library sorting in function c++ sort c++ stl array sorting in c++ cpp sort sort array in c++ c++ acending sort algo sort method c++ how to sort an interger array in c++ sort an array in c++ sort an array C++ h sort c++ sort a c++ array .sort() c++ sorting array inc++ sort ing in c++ how to sort an array c++ sorting in c++ c++ int array sort sort in c++ c++ sort library sort function in c++ sort function c++ how to sort an array in cpp c++ sort int array sorting header file in c++ what header is sort in c++ sorting an array c++ sort c++ comp example sort algorithm c++ c++ algorithm sort sort() C++ sort cpp sort array c++ example sort library c++ c++ sort function how to sort an array in c++ how to sort array in c++ c++ sort array of ints c++ vector sort c++ sorting sort c++ c++ stl sort c++ sort
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source