How to sort an array in a single loop?

后端 未结 22 2750
面向向阳花
面向向阳花 2020-12-19 09:14

So I was going through different sorting algorithms. But almost all the sorting algorithms require 2 loops to sort the array. The time complexity of Bubble sort & Insert

相关标签:
22条回答
  • 2020-12-19 09:45

    This can be used to sort array usinga single loop:- Points to be noed:

    1. updating the value of i to -1 so that it alwasy starts from 0 after i++
    2. reducing the length(size--) of array as maximum valued element ends up at the end for every time the loop completes

    Code:

    void sort(int *arr,int size){
        int i;
        for (i = 0; i <size; i++){
            if(arr[i]>arr[i+1]){
                arr[i]=arr[i]+arr[i+1];
                arr[i+1]=arr[i]-arr[i+1];
                arr[i]=arr[i]-arr[i+1];
                if(i==size-2){
                    printf("%s\n","inside if loop" );
                    i=-1;
                    size--;
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-19 09:46

    In the general case you have O(n lg n) as an average.

    But in particular cases, the best case is O(n), which I consider close enough to what you'd call "only one loop", even though the implementation may show more than one instance of the for keyword. And the good news with that, is that you're not depending on luck to make your best case a reality. Provided you know a few properties about your data, you can pick some specific algorithms. For example :

    • 3-way quicksort runs very near O(n) when you have a lot of items with only a few distinct sorting keys (think server log entries as items and dates as keys).
    • Counting sort runs in O(n+k) if your keys are easily indexable (like a character set, or small integers), and the index has a known upper bound k.
    • Burstsort will run in O(wn) if you're dealing with strings of maximum length w.

    Those are but three examples. There are many more, way too many to recall from the top of my head, for many types of constrained data sets. If you have a real-life case at hand where O(n lg n) is not good enough, it's well worth doing some proper research, provided you identified a few interesting properties in your data.

    0 讨论(0)
  • 2020-12-19 09:46

    public class SinleLoopeSorting {

    public static void main(String[] args) {
        Integer[] x = new Integer[] { 1, 7, 8, 0, 4, 2, 3 };
    
        for (int i = 0; i < x.length - 1; i++) {
            if (x[i] > x[i + 1]) {
                int p = x[i];
                x[i] = x[i + 1];
                x[i + 1] = p;
                i = -1;
            }
        }
        for (int i = 0; i < x.length; i++) {
            System.out.println(x[i]);
        }
    }
    

    }

    0 讨论(0)
  • 2020-12-19 09:46

    Single for loop for insertion sort:

    strong text

    function insertionSort (array) {
        for(var i = 1 ; i < array.length ;){
            if(array[1] < array[0]) {
                    temp = array[i];
                    array[i] = array[i -1];
                    array[i -1] = temp; 
             }
            if(array[i] < array[i-1]){
                var temp = array[i]
                array[i] = array[i -1]
                array[i -1] = temp
                i--
            } else{i++}
        }
        return array
    }
    
    0 讨论(0)
提交回复
热议问题