📜  排序双音阵列

📅  最后修改于: 2021-04-22 02:39:18             🧑  作者: Mango

给定一个双音数组arr []的任务是对给定的双音数组进行排序。

例子:

方法:

  • 这个想法是将变量K初始化为数组大小的2最高幂,以便比较相距K的元素。
  • 如果元素不按升序交换,请交换它们。
  • K减小一半,然后重复该过程,直到K变为零。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
  
// Function to Sort a Bitonic array
// in constant space
void sortArr(int a[], int n)
{
    int i, k;
  
    // Initialse the value of k
    k = (int)log2(n);
    k = pow(2, k);
  
    // In each iteration compare elements
    // k distance apart and swap if
    // they are not in order
    while (k > 0) {
        for (i = 0; i + k < n; i++)
            if (a[i] > a[i + k])
                swap(a[i], a[i + k]);
  
        // k is reduced to half
        // after every iteration
        k = k / 2;
    }
  
    // Print the array elements
    for (i = 0; i < n; i++) {
        cout << a[i] << " ";
    }
}
  
// Driver Code
int main()
{
    // Given array arr[]
    int arr[] = { 5, 20, 30, 40, 36, 33, 25, 15, 10 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    // Function Call
    sortArr(arr, n);
  
    return 0;
}


Java
// Java program for the above approach 
import java.io.*;
  
class GFG{
      
// Function to Sort a Bitonic array 
// in constant space 
static void sortArr(int a[], int n) 
{ 
    int i, k; 
  
    // Initialse the value of k 
    k = (int)(Math.log(n) / Math.log(2)); 
    k = (int) Math.pow(2, k); 
  
    // In each iteration compare elements 
    // k distance apart and swap if 
    // they are not in order 
    while (k > 0) 
    { 
        for(i = 0; i + k < n; i++) 
            if (a[i] > a[i + k])
            {
                int tmp = a[i];
                a[i] = a[i + k];
                a[i + k] = tmp;
            }
  
        // k is reduced to half 
        // after every iteration 
        k = k / 2; 
    } 
  
    // Print the array elements 
    for(i = 0; i < n; i++)
    { 
        System.out.print(a[i] + " "); 
    } 
}
      
// Driver code
public static void main (String[] args) 
{
      
    // Given array arr[] 
    int arr[] = { 5, 20, 30, 40, 36, 
                  33, 25, 15, 10 }; 
    int n = arr.length; 
      
    // Function call 
    sortArr(arr, n); 
}
}
  
// This code is contributed by offbeat


Python3
# Python3 program for the
# above approach
import math
  
# Function to sort bitonic
# array in constant space
def sortArr(a, n):
  
    # Initialize thevalue of k
    k = int(math.log(n, 2))
    k = int(pow(2, k))
  
    # In each iteration compare elements
    # k distance apart and swap it
    # they are not in order
    while(k > 0):
        i = 0
        while i + k < n:
            if a[i] > a[i + k]:
                a[i], a[i + k] = a[i + k], a[i]
            i = i + 1
              
        # k is reduced to half after
        # every iteration
        k = k // 2
      
    # Print the array elements     
    for i in range(n):
        print(a[i], end = " ")
  
# Driver code
  
# Given array
a = [ 5, 20, 30, 40, 36, 33, 25, 15, 10 ]
n = len(a)
  
# Function call
sortArr(a, n)
  
# This code is contributed by virusbuddah_


C#
// C# program for the above approach 
using System;
  
class GFG{
      
// Function to Sort a Bitonic array 
// in constant space 
static void sortArr(int []a, int n) 
{ 
    int i, k; 
  
    // Initialse the value of k 
    k = (int)(Math.Log(n) / Math.Log(2)); 
    k = (int) Math.Pow(2, k); 
  
    // In each iteration compare elements 
    // k distance apart and swap if 
    // they are not in order 
    while (k > 0) 
    { 
        for(i = 0; i + k < n; i++) 
            if (a[i] > a[i + k])
            {
                int tmp = a[i];
                a[i] = a[i + k];
                a[i + k] = tmp;
            }
  
        // k is reduced to half 
        // after every iteration 
        k = k / 2; 
    } 
  
    // Print the array elements 
    for(i = 0; i < n; i++)
    { 
        Console.Write(a[i] + " "); 
    } 
}
      
// Driver code
public static void Main(String[] args) 
{
      
    // Given array []arr 
    int []arr = { 5, 20, 30, 40, 36, 
                  33, 25, 15, 10 }; 
    int n = arr.Length; 
      
    // Function call 
    sortArr(arr, n); 
}
}
  
// This code is contributed by amal kumar choubey


输出:
5 10 15 20 25 30 33 36 40

时间复杂度: O(N * log N)
辅助空间: O(1)