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] << " " ;
	}
		
}

Recommended Answers

All 3 Replies

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.

If you are looking to learn more about sorting, look up "Sorting Algorithm".

Most importantly, test your own code. Giving it random input is a start. Then, you should try to guess what the worst possible input could be ( the input that is hardest for you algorithm ) and test it on that.

>There are no known sorting algorithms that perform in linear time.
There are no known comparison-based sorting algorithms because the limit with a comparison tree is Omega(nlogn). But linear algorithms do exist using other methods, such as distribution.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.