Link to home
Start Free TrialLog in
Avatar of HardSoft
HardSoft

asked on

sorting algorithms

i am new to programing
hai friends
i want some good examples on sorting my friend gave a small idea like
   for(i=0;i<n;i++)
   {
     for(j=0;j<n;j++)
     {
       if(condition)swap(a[i],a[j]);
      }
    }

it is working and i found it is not better algorithm

by replacing the code with
   for(i=0;i<n;i++)
   {
     for(j=0;j<=i;j++)
     {
       if(condition)swap(a[i],a[j]);
      }
    }
and i tried this code also

   for(i=0;i<n;i++)
   {
     for(j=i;j<n;j++)
     {
          if(condition)swap(a[i],a[j]);
      }
    }

for first algoritm it is going upto n^2 iterations and my ideas are going upto sigma(n) iterations

are there any good logics to reduce itertions further
Avatar of HardSoft
HardSoft

ASKER

Thanks in advance
ASKER CERTIFIED SOLUTION
Avatar of sudhakar_koundinya
sudhakar_koundinya

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thank you i will try these ideas

could any body give me algorithmic procedures(not examples) that sudhakar  suggested
There is no algorythmic way of reducing iteration on a unknown algorythm. If there was one, we would have self optimizing programms and the man who invented it would be very rich.
But sorting algorithms have been around for pretty long. And many people have squezed as much iterations out of it as might be possible. To understand those algorithms how they work and why they or work faster than your intial example, is subject for most computer engineer students.
I'm one of them and we were tought by the book of Robert Sedgewickge "Algorithms in C". I can recomend this book. It gives a indepth look into algorithms in general and sorting algortihms in specific. There will be a "Algorithms in Java" but not before 2001 as I was told. But maybe you'd like to have a close look at the C Examples anyway, since they translate easly to Java. Here it is the book on amazon: http://www.amazon.com/exec/obidos/ASIN/0201514257/qid=964314228/sr=1-1/104-1774705-1506343
Due to copyright restrictions, I'm not allowed to scan, OCR and publish the chapters you are interested of my book of Sedgewick. But be assure you that the book is a worthy investment :)
The worst-case fastest sorting algorithm is merge-sort (nlogn) and the fastest average case sorting algorithm is quick-sort (also nlogn, but less operations in average, n^2 in worst case). You can bye the book MadMike suggested (preferable) or look up any of these algorithms on the web (more cost efficient :-) ).

Sasha.
Sudhakar Koundinya:
I just wanted to say: more power to you. Working this hard for 33 pts, you are a very good person, I can tell.

:-)

HardSoft:
Buy a book called "Introduction to algorithms" by Coremenn.

Cheers
Comment accepted as answer