Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting an array in C?

Which is the best sorting technique to sort the following array and if there are duplicates how to handle them:

int a= {1,3,6,7,1,2}; 

Also which is the best sorting technique of all?

void BubbleSort(int a[], int array_size) {     int i, j, temp;     for (i = 0; i < (array_size - 1); ++i)     {         for (j = 0; j < array_size - 1 - i; ++j )         {             if (a[j] > a[j+1])             {                 temp = a[j+1];                 a[j+1] = a[j];                 a[j] = temp;             }         }     } } 
like image 903
Rajeev Avatar asked Oct 08 '10 20:10

Rajeev


People also ask

What is sorting of an array?

A sorted array is an array data structure in which each element is sorted in numerical, alphabetical, or some other order, and placed at equally spaced addresses in computer memory. It is typically used in computer science to implement static lookup tables to hold multiple values which have the same data type.

Is there a sort method in C?

The various types of sorting methods possible in the C language are Bubble sort, Selection sort, Quick sort, Merge sort, Heap sort and Insertion sort.

What is sort operation on an array in C?

This operation is performed to sort an Array into a fixed order, i.e., either ascending or descending. Here is an example of sort operation on an Array in C Below are the different sorting methods for Array: Bubble sort compares all the elements one by one and sorts them based on their values.

How to sort a range of elements in an array?

The Sort.Array method allows you to sort a range of elements within an array. For example, if an array has 11 elements but what if you want to sort only 1st to next 6 elements in the array? You can use that by passing the first starting index of the element followed by the number of elements to be sorted.

How to sort an array of integers in JavaScript?

The Array.Sort method takes a one-dimensional array as an input and sorts the array elements in the ascending order. The following code snippet creates an array of integers. The Array.Sort method takes array as an input and sorts the array in ascending order. To sort an array in descending order, we can use Sort.Reverse method.

Can you sort an array in ascending and descending order?

We are about to discuss the sorting of an array in ascending as well as descending order. Sorting of data basically means arranging or organizing the data in a particular fashion. There are numerous algorithms available but we are using selection sort because its basic, simple and easy.


2 Answers

In C, you can use the built in qsort command:

int compare( const void* a, const void* b) {      int int_a = * ( (int*) a );      int int_b = * ( (int*) b );       if ( int_a == int_b ) return 0;      else if ( int_a < int_b ) return -1;      else return 1; }  qsort( a, 6, sizeof(int), compare ) 

see: http://www.cplusplus.com/reference/clibrary/cstdlib/qsort/


To answer the second part of your question: an optimal (comparison based) sorting algorithm is one that runs with O(n log(n)) comparisons. There are several that have this property (including quick sort, merge sort, heap sort, etc.), but which one to use depends on your use case.

As a side note, you can sometime do better than O(n log(n)) if you know something about your data - see the wikipedia article on Radix Sort

like image 103
Alex Reece Avatar answered Sep 18 '22 21:09

Alex Reece


In your particular case the fastest sort is probably the one described in this answer. It is exactly optimized for an array of 6 ints and uses sorting networks. It is 20 times (measured on x86) faster than library qsort. Sorting networks are optimal for sort of fixed length arrays. As they are a fixed sequence of instructions they can even be implemented easily by hardware.

Generally speaking there is many sorting algorithms optimized for some specialized case. The general purpose algorithms like heap sort or quick sort are optimized for in place sorting of an array of items. They yield a complexity of O(n.log(n)), n being the number of items to sort.

The library function qsort() is very well coded and efficient in terms of complexity, but uses a call to some comparizon function provided by user, and this call has a quite high cost.

For sorting very large amount of datas algorithms have also to take care of swapping of data to and from disk, this is the kind of sorts implemented in databases and your best bet if you have such needs is to put datas in some database and use the built in sort.

like image 42
kriss Avatar answered Sep 19 '22 21:09

kriss