The Index Sorting Algorithm
The Index Sorting Algorithm
Volume: 4 Issue: 3
ISSN: 2321-8169
51 - 54
_______________________________________________________________________________________
AbstractEfficient sorting and searching are corner-stones in algorithm design. In computer science it has become a deep-rooted habit to use
comparison-based methods to solve these problems. In this research paper we have come up with a new algorithm which we have named The
Index Sorting Algorithm, that sorts given list of elements in the array in O(n) time and O(n) space complexity in worst case, better than any
other sorting algorithm. The algorithm is very easy to implement and understand.
Keywords-sorting algorithm; sorting technique; time complexity; space complexity
__________________________________________________*****_________________________________________________
I.
INTRODUCTION
Sorting Algorithm
and Bucket Sorts take O(n) time ,but they ask for input
restrictions(only integers allowed as input) and cannot sort all
the input given. But our algorithm is able to sort any input (not
asking for any input restrictions) in O(n) time complexity and
O(n) space complexity. Any of the above mentioned
algorithms are not capable to throw the desired result in O(n)
time. In order to implement this algorithm we have used an
array mapping with the input numbers, and only by doing
three scans we are able to sort any kind of input (no restriction)
provided to us.
III.
2 4 5 6 8 8 12 15
Figure 1. Sorting Technique.
II.
4.
SORTING ALGORITHMS
5.
6.
_______________________________________________________________________________________
ISSN: 2321-8169
51 - 54
_______________________________________________________________________________________
maximum, once to read the elements one by one and change
the array content accordingly and once to read the array and
display the elements i.e. three scans of n elements O(3n)
which is O(n).
The comparison between the Index Sorting Algorithm and
other algorithms is given in the next section.
U n s o r te d
E l e me n t s
Array
Index
Array
Va l u e
12
5
6
15
10
11
12
13
14
15
S o r te d
E l e me n t s
8, 8
12
15
for(i=1;i<n;i++)
{
if (min>a[i])
{min=a[i];}
if (max<a[i])
{max=a[i];}
}int*index=(int*)calloc((max+1),sizeof(int));
//for accomodating 0 also max+1
for (i=0;i<n;i++)
{index[a[i]]++;}
//store the count of occurances of the element
cout<<endl;
for (i=0;i<max+1;i++)
{if (index[i]>0)
{cout<<i<<'\t';}
j=index[i];
//used to print duplicated element more than once.
if (j==1 || j==0)continue;
while(j!=1)
{cout<<i<<'\t';//print the duplicates
j--;}}}
int main()
{int a[]={4,1,0,5,3,2,34, 8, 10, 3, 2, 80, 30, 33, 1,9, 2, 3, 4, 5, 6,
7, 8};
int s=sizeof(a)/sizeof(int);
//size of the input array
indexSort(a,s);
//function call to Sorting function
Return0;}
B. The Selection Sort Algorithm
Given below is the code for the Selection Sort Algorithm.
IV.
#include <stdio.h>
void swap(int *xp, int *yp)
{int temp = *xp;
*xp = *yp;
*yp = temp;}
void selectionSort(int arr[], int n)
{ int i, j, min_idx;
// One by one move boundary of unsorted subarray
for (i = 0; i < n-1; i++)
{// Find the minimum element in unsorted array
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
// Swap the found minimum element with the first
element
swap(&arr[min_idx], &arr[i]);}}
/* Function to print an array */
void printArray(int arr[], int size)
{ int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");}
// Driver program to test above functions
int main()
{ int arr[] ={4,1,0,5,3,2,34, 8, 10, 3, 2, 80, 30, 33, 1,9, 2, 3, 4,
5, 6, 7, 8,};
int n = sizeof(arr)/sizeof(arr[0]);
selectionSort(arr, n);
52
_______________________________________________________________________________________
ISSN: 2321-8169
51 - 54
_______________________________________________________________________________________
printf("Sorted array: \n");
printArray(arr, n);
return 0;}
The Index Sort Algorithm is much faster as compared to
selection sort algorithm. It is faster by .100 seconds for an
input of 24 array elements. This difference increases as the
number of elements increase.
C. The Merge Sort Algorithm
Given below is the code for the Merge Sort Algorithm.
#include<iostream
using namespace std;
void merge(int arr[], int l, int m, int r)
{ int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
/* create temp arrays */
int L[n1], R[n2];
/* Copy data to temp arrays L[] and R[] */
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1+ j];
/* Merge the temp arrays back into arr[l..r]*/
i = 0; j = 0; k = l;
while (i < n1 && j < n2)
{if (L[i] <= R[j])
{ arr[k] = L[i]; i++; }
else{arr[k] = R[j]; j++;}
k++;}
/* Copy the remaining elements of L[], if there are any */
while (i < n1)
{arr[k] = L[i];
i++; k++;}
/* Copy the remaining elements of R[], if there are any */
while (j < n2)
{arr[k] = R[j];
j++; k++;}}
void msort(int a[],int i,int j)
{int m;
if(i<j)
{m=i+((j-i)/2);
msort(a,i,m);
msort(a,m+1,j);
merge(a,i,m,j);}}
int main()
{int k;
int arr[] ={4,1,0,5,3,2,34, 8, 10, 3, 2, 80, 30, 33, 1,9, 2, 3, 4,
5, 6, 7, 8};
int s=sizeof(arr)/sizeof(int);
msort(arr,0,s);
cout<<"The sorted array is";
cout<<endl;
for (k=0;k<s;k++)
{cout<<arr[k]<<"-";}
cout<<endl;}
We observe from the output that our algorithm is faster
than merge sort algorithm by 0.008 seconds and this difference
real 0m0.179s
b.
user
c.
sys 0m0.044s
0m0.135s
real 0m0.187s
b.
user
c.
sys 0m0.044s
0m0.132s
real 0m0.270s
b.
user 0m0.131s
c.
sys 0m0.045s
CONCLUSION
2.
3.
Disadvantages:
1. It requires extra space as per input
provided.
Wastage of space if the input is short and has large
number. For example {1,890,56,345} unnecessarily an array
of 890 size needs to be created to sort just four elements.
53
_______________________________________________________________________________________
ISSN: 2321-8169
51 - 54
_______________________________________________________________________________________
REFERENCES
[1]
[2]
[3]
[4]
[5]
[6]
TABLE I.
Algorithm
Complexity
Space
Bubble sort
Bucket sort
Merge sort
Quick sort
Heap sort
Insertion sort
Radix sort
Selection sort
Bucket sort
Index sort
Worst
O(1)
O(n)
O(n)
O(log(n))
O(1)
O(1)
O(n+k)
O(1)
O(1)
O(n)
Time
Best
O(n)
O(n+k)
O(nlog(n))
O(nlog(n))
O(nlog(n))
O(n)
O(nk)
O(n2)
O(n)
O(n)
Average
O(n2)
O(n+k)
O(nlog(n))
O(nlog(n))
O(nlog(n))
O(n2)
O(nk)
O(n2)
O(n2)
O(n)
Worst
O(n2)
O(n2)
O(nlog(n))
O(n2)
O(nlog(n))
O(n2)
O(nk)
O(n2)
O(n2)
O(n)
54
IJRITCC | March 2016, Available @ http://www.ijritcc.org
_______________________________________________________________________________________