12.basic sorting
21 May 2020 | STL Programming Practice_1
basic sorting by STL
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int main()
{
int A[] = {1,4,2,0,5,10,9};
int n = sizeof(A)/sizeof(A[0]);
// sort(first position, last position+1, comparision function)
sort(A+0,A+n);
for(int i=0; i<=n-1; i++)
{
cout<<A[i]<<" ";
}
return 0;
}
basic sorting by STL with comparison function(optional)
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
bool cmd(int X, int Y)
{
return X>Y;
}
int main()
{
int A[] = {1,4,2,0,5,10,9};
int n = sizeof(A)/sizeof(A[0]);
// sort(first position, last position+1);
sort(A+0,A+n,cmd);
for(int i=0; i<=n-1; i++)
{
cout<<A[i]<<" ";
}
return 0;
}
basic sorting by STL
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int main()
{
int A[] = {1,4,2,0,5,10,9};
int n = sizeof(A)/sizeof(A[0]);
// sort(first position, last position+1, comparision function)
sort(A+0,A+n);
for(int i=0; i<=n-1; i++)
{
cout<<A[i]<<" ";
}
return 0;
}
basic sorting by STL with comparison function(optional)
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
bool cmd(int X, int Y)
{
return X>Y;
}
int main()
{
int A[] = {1,4,2,0,5,10,9};
int n = sizeof(A)/sizeof(A[0]);
// sort(first position, last position+1);
sort(A+0,A+n,cmd);
for(int i=0; i<=n-1; i++)
{
cout<<A[i]<<" ";
}
return 0;
}
Comments