How to sort an array in a single loop?

后端 未结 22 2749
面向向阳花
面向向阳花 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:31

    Single Loop Bubble Sort using C++

    int a[7]={5,7,6,2,4,3,1};
    
    int temp = 0;
    
    int j = 0;
    
    for(int i = 0 ; i<a[]-1 ; i++)
    {
        int flag = 0;
        if(a[i]>a[i+1])
        {
            temp = a[i];
            a[i] = a[i+1];
            a[i+1] = temp;
            flag = 1;
        }       
        if(i == 7-2-j)
        {
            if(!flag) break;
            i = -1;
            j++;
        }
    }
    
    0 讨论(0)
  • 2020-12-19 09:32

    Late to the party but hope this helps

    java solution

    for(int i=1;i< arr.length;i++) {
            if(arr[i] < arr[i-1] ){
                arr[i-1] += arr[i];
                arr[i] = arr[i-1] - arr[i];
                arr[i-1] -= arr[i];
                i=0;
            }
    }
    
    0 讨论(0)
  • 2020-12-19 09:33

    Here, a single-loop Bubble Sort in Python:

    def bubbly_sortish(data):
        for _ in xrange(len(data)**2):
            i, j = _/len(data), _%len(data)
            if i<j and data[i] > data[j]:
                data[i], data[j] = data[j], data[i]
    
    A = [5, 1, 2, 3, 5, 6, 10]
    bubbly_sortish(A)
    
    print A            
    

    Of course this is a joke. But this shows the number of loops has little to do with algorithm complexity.

    Now, if you're asking if it is possible to sort an array with O(n) comparisons, no, it's not possible. The lower bound is Ω(n log n) for comparison-based sorting algorithms.

    0 讨论(0)
  • 2020-12-19 09:33
    int list[] = { 45, 78, 22, 96, 10, 87, 68, 2 };
        for (int i = 1; i < list.length; i++) {
            if (list[i] < list[i - 1]) {
                list[i] = list[i] + list[i - 1];
                list[i - 1] = list[i] - list[i - 1];
                list[i] = list[i] - list[i - 1];
                i = 0;
            }
        }
        System.out.print("Sorted array is : ");
        for (int i = 0; i < list.length; i++) {
            System.out.print(list[i] + " ");
        }
    
    0 讨论(0)
  • 2020-12-19 09:33

    Single loop array sort:

    for(int i = 0, j=i+1; i < arr.length && j<arr.length;)
        {       
            if(arr[i] > arr[j])
            {
                int temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;              
                i=0;
                j=i+1;
            } 
            else
            {
                i++;
                j++;
            }
        }
    
    0 讨论(0)
  • 2020-12-19 09:37
    public void sortArrayUsingSingleLoop(int[] intArray) {
            int temp;
            boolean swap = false;
            for(int i=0;i<intArray.length-1;i++){
             if(intArray[i]>intArray[i+1]){
                 temp=intArray[i];
                 intArray[i]=intArray[i+1];
                 intArray[i+1]=temp;
                 swap=true;
             }
    
             if(swap &&i==intArray.length-2){
                i=-1;swap=false;
             }
            }
    
        }
    
    0 讨论(0)
提交回复
热议问题