### Best way to sort an array

This is the best idea og algorithm for sorting an array that I could come up with.
But I read on internet that there are many other ways of strting an array, but didn't found how the algorithms work. So if you know a better or just different way of sorting an array please post.
 ``123456789101112`` ``````void mySort(int a[], unsigned n) { for(unsigned k = n-1, x = 0; x!=k; x++) for(unsigned m = x, y = x+1; y!=n; y++) if(a[x] > a[y]) m = i; if(m != n) { int h = a[x]; a[x] = a[m]; a[m] = h; } }``````
C++ knows how to sort arrays already, no need to write anything special:
 ``1234`` ``````void mySort(int a[], unsigned n) { std::sort(a, a+n); }``````
Cubbi's solution's best if you are writing code whose main purpose is not sorting. However, if you want to write a sorting algorithm for your own practice, there are many more algorithms much faster than yours.

How fast an algorithm is measured by the big O notation. The algorithm you have written is called Selection Sort. It's speed is O(n^2), i.e. time taken is proportional to square of size of array.

The easiest algorithms have speed O(n^2) while a bit difficult but faster ones have speed O(n*log n).

List of some easy sorting algorithms (you can get their working on wikipedia):
Bubble sort O(n^2) http://en.wikipedia.org/wiki/Bubble_sort
Insertion sort O(n^2)
Merge sort O(nlogn)
Ok, so I finaly decided to write functions for sorting an array.
I have wrote the functions for sorting then the number of calculated arithmetic, logical and comparision operations and asignments for a givven array (and displayed it) and got to a conclusion that they are all about the same speed.
But when i got to merge sort I got a bit confused by the animation on wikipedia http://upload.wikimedia.org/wikipedia/commons/c/cc/Merge-sort-example-300px.gif
It shows how it is done with array of lenght of 8, but what if it can not be devaded by 2? What if it is 3, 7, 12?

Take a look of what I wrote:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112`` ``````#include #include using std::cout; using std::cin; void copy(int* a, int n); void selectionSort(int* a, int n); void doubleSort(int* a, int n); void insertionSort(int* a, int n); void mergeSort(int* a, int n); main() { int n; do { cout << "Koliko elemenata? "; cin >> n; } while (n<2); int *a = new int[n], *s = new int[n]; if(n>4) cout << "Unesite " << n << " celih brojeva:\n"; else cout << "Unesite " << n << " cela broja:\n"; for(int x = 0; x!=n; x++) cin >> a[x]; cout<<'\n'; for(int x = 0; x a[y]) { m = y; b++; } } if(x != m) { int h = a[x]; a[x] = a[m]; a[m] = h; b += 3; } } for(int x = 0; x!=n; x++) cout << a[x] << " "; cout << "\nZa sortiranje ovog niza Selection Sort algoritmom uradjeno je " << b << " operacija.\n\n"; } void doubleSort(int* a, int n) { int b = 3; for(int x = n-1; x>1; x--) { b += 4; for(int y = 1; y <= x; y++) { b += 5; int u = y-1; if(a[y] < a[u]) { int h = a[y]; a[y] = a[u]; a[u] = h; b += 3; } } } for(int x = 0; x!=n; x++) cout << a[x] << " "; cout << "\nZa sortiranje ovog niza Double Sort algoritmom uradjeno je " << b << " operacija.\n\n"; } void insertionSort(int* a, int n) { int b = 2; for(int m, x = 1; x
Last edited on
The animation didn't seem confusing to me. It might seem confusing to you if you don't know what merging is.

This is the first time I'm writing code for merge sort.
There is a problem: I need extra memory. I can't use new as this will slow down the algorithm. Hence, I'm using templates to tackle the problem.

http://www.cplusplus.com/doc/tutorial/templates/
(in case you don't know what templates are)

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950`` ``````#include using namespace std; void merge(int* a,int na,int* b,int nb,int* c) { int pa=0,pb=0,pc=0; while(pa void mergesort(int* a) { const int k=n/2; int *b,c[n],i; b=a+k; mergesort(a); mergesort(b); merge(a,k,b,n-k,c); for(i=0;i void mergesort<1>(int* a){} template<> void mergesort<2>(int* a) { int t; if(a[0]>a[1]) {t=a[0];a[0]=a[1];a[1]=t;} } int main() { const int n=8; int i,a[n]; for(i=0;i>a[i]; } mergesort(a); for(i=0;i

I've tested this code and it runs.
The problem was easy but not as easy as I thought.

There's a probablity that you don't know what templates are and I should've used new. But finding new related bugs is harder and I'm more comfortable with templates and I am feeling very sleeeeeeeeeeeepy. Good night.
Last edited on
That still sorts 8 elements...
change the value of n (in main) to whatever you like.
So, I looked at your code and I think I get it, but I have a problem.
Apparently standard only allows constant value to be passed for template when calling mergesort<n>(a);
How do I first input value for n then pass it to the template?
Well you can do that in my example by dynamically allocating arrays and taking n as a parameter instead of a template parameter. I think that this approach will make the sorting process slower as dynamic allocation and deallocation take a lot of time.

There are more than one ways to merge 2 sorted arrays into a single sorted array. The way I have used for merging is not really fit for this purpose. I presently don't know other ways to do this.
Ok, I'll use if else instead of template...
Topic archived. No new replies allowed.