Quantcast
Channel: CodeChef Discuss - latest questions
Viewing all articles
Browse latest Browse all 39796

New Sorting Algorithm

$
0
0

I study in class 12, India. I have developed a new sorting algorithm. I dont have much knowledge about the names of sorting technique, ignore if you know this technique.

Let the array is a[].

int minIndex,maxIndex;
void sort(int a[])
{
    int l = 0,r = a.length-1,t = 0;
    while(l < r) {
        searchMinMax(a,l,r);     
        t = a[l];               
        a[l] = a[minIndex];
        a[minIndex] = t;
        t = a[r];
        a[r] = a[maxIndex];
        a[maxIndex] = t;
        l++;
        r--;
    }
}
void searchMinMax(int a[],int l,int r)
{
    int max=Integer.MIN_VALUE,min=Integer.MAX_VALUE;
    while(l < r)
    {
        if(a[l] < a[r])
        {
            if(a[r] > max)
            {
                max = a[r];
                maxIndex = r;
            }
            if(a[l] < min)
            {
                min = a[l];
                minIndex = l;
            }
        }
        else
        {
            if(a[l] > max)
            {
                max = a[l];
                maxIndex = l;
            }
            if(a[r] < min)
            {
                min = a[r];
                minIndex = r;
            }
        }
        l++;
        r--;
    }
}

It is working very fast and efficiently. All comments are welcomed.



Viewing all articles
Browse latest Browse all 39796

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>