invisi

Well I wrote this programme, hoping what I wrote is a lenear sorting algorithm

if it's not pls tell me :-O

``````#include <iostream>
#include <string>

using namespace std;

int main()
{
int a[10];

int num = 0;
int number = 0;
cout << "Enter 10 int..." << endl;
for (int i = 0; i < 10; i++)
{
cout << i << " : ";
cin >> number;
a[i] = number;

}
cout << endl;
for (int j = 0; j < 10; j++)
{
for (int i = 0; i < 10; i++)
{
if(a[j] < a[i - 1])
{
num = a[i - 1];
a[i - 1] = a[j];
a[j] = num;
}
else continue;
}

}
for (int i = 0; i < 10; i++)
{

cout << a[i] << " " ;
}

}``````

pecet 1

That's what I I have found at first look:
lines#24-26:

``````for (int i = 0; i < 10; i++)
{
if(a[j] < a[i - 1])``````

if `i=0` and you have `a[i-1]` , then You get `a[-1]` . That's the problem You have to solve.

Well I wrote this programme, hoping what I wrote is a lenear sorting algorithm

if it's not pls tell me :-O

``````for (int j = 0; j < 10; j++)
{
for (int i = 0; i < 10; i++)
{
//some operation...
}
}
}}``````

It's not. I assume that you mean linear in time complexity? You should probably save your effort there. There are no known sorting algorithms that perform in linear time. First, your sort wont work (which you can verify by testing it yourself on random data). Secondly, your time complexity here is obviously O(n^2) which is poor performance for a sort. Basically, this sort can perform no less than n^2 operations for n inputs.

If you need a sorting algorithm, check out the standard template library. The sort is very efficient, as it is an implementation of the Introsort algoirthm. It's time complexity is O( nlogn ), which is about the best you can do with a sorting algorithm.