faster execution time?

I was somehow practicing c++ after a long time (learning some high school physics and JS these days) when I tried to sort a array in ascending order. I came up with a solution which was like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
cout<<"enter the max. limit of array: ";

int l;

cin>>l;
int a[100],t1,t2;

cout<<"Enter the array: \n";

for(int i=0;i<l;i++)
cin>>a[i];

cout<<"\narray after sorting: ";
for(i=0;i<l;i++)
	{
		for(int k=0;k<i;k++)

		{

		if(a[i]<a[k])
			{
			t1=a[i];
			t2=a[k];
			a[i]=t2;
			a[k]=t1;
			}

		}
	}

for(i=0;i<l;i++)
cout<<a[i]<<' ';


Now I was wondering whether there is a faster alternate to this problem... What would that be?

Also it would be great if you could explain how...
Last edited on
bump.....
See: https://en.wikipedia.org/wiki/Sorting_algorithm

std::sort() in <algorithm> guarantees a worst-case complexity of O(n log n)
The introduction of hybrid algorithms such as introsort allowed both fast average performance and optimal worst-case performance, and thus the complexity requirements were tightened in later standards.
https://en.wikipedia.org/wiki/Sort_(C%2B%2B)

Edit: for a small sequence (100), there wouldn't be any measurable difference in performance.
Last edited on
Thanks @JLBorges
Topic archived. No new replies allowed.