📜  使用循环排序对O(N)中包含1到N个值的数组进行排序

📅  最后修改于: 2021-05-14 01:21:25             🧑  作者: Mango

先决条件:循环排序
给定一个从1到N的元素数组arr [] ,任务是按照O(N)时间对给定的数组进行排序。
例子:

方法:此问题可以使用贪婪方法解决。步骤如下:

  • 遍历给定数组arr []
  • 如果当前元素不在正确的位置,即arr [i]不等于i + 1 ,则将当前元素与其位置正确的元素交换。
    例如:
  • 如果当前元素在正确的位置,则检查下一个元素。
  • 重复上述步骤,直到到达数组末尾。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include "bits/stdc++.h"
using namespace std;
 
// Function to swap two a & b value
void swap(int* a, int* b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}
 
// Function to print array element
void printArray(int arr[], int N)
{
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
        cout << arr[i] << ' ';
    }
}
 
// Function to sort the array in O(N)
void sortArray(int arr[], int N)
{
 
    // Traverse the array
    for (int i = 0; i < N;) {
 
        // If the current element is
        // at correct position
        if (arr[i] == i + 1) {
            i++;
        }
 
        // Else swap the current element
        // with it's correct position
        else {
            swap(&arr[i], &arr[arr[i] - 1]);
        }
    }
}
 
// Driver Code
int main()
{
 
    int arr[] = { 2, 1, 5, 3, 4 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function call to sort the array
    sortArray(arr, N);
 
    // Function call to print the array
    printArray(arr, N);
    return 0;
}


Java
// Java program for the above approach
class Main{
     
// Function to print array element
public static void printArray(int arr[], int N)
{
     
    // Traverse the array
    for(int i = 0; i < N; i++)
    {
       System.out.print(arr[i] + " ");
    }
}
     
// Function to sort the array in O(N)
public static void sortArray(int arr[], int N)
{
 
    // Traverse the array
    for(int i = 0; i < N;)
    {
 
       // If the current element is
       // at correct position
       if (arr[i] == i + 1)
       {
           i++;
       }
        
       // Else swap the current element
       // with it's correct position
       else
       {
           // Swap the value of
           // arr[i] and arr[arr[i]-1]
           int temp1 = arr[i];
           int temp2 = arr[arr[i] - 1];
           arr[i] = temp2;
           arr[temp1 - 1] = temp1;
       }
    }
}
 
// Driver Code   
public static void main(String[] args)
{
    int arr[] = { 2, 1, 5, 3, 4 };
    int N = arr.length;
 
    // Function call to sort the array
    sortArray(arr, N);
 
    // Function call to print the array
    printArray(arr, N);
}
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 program for the above approach
 
# Function to print array element
def printArray(arr, N):
     
    # Traverse the array
    for i in range(N):
        print(arr[i], end = ' ')
         
# Function to sort the array in O(N)
def sortArray(arr, N):
     
    i = 0
     
    # Traverse the array
    while i < N:
         
        # If the current element is
        # at correct position
        if arr[i] == i + 1:
            i += 1
         
        # Else swap the current element
        # with it's correct position
        else:
             
            # Swap the value of
            # arr[i] and arr[arr[i]-1]
            temp1 = arr[i]
            temp2 = arr[arr[i] - 1]
            arr[i] = temp2
            arr[temp1 - 1] = temp1
     
# Driver code
if __name__=='__main__':
     
    arr = [ 2, 1, 5, 3, 4 ]
    N = len(arr)
     
    # Function call to sort the array
    sortArray(arr, N)
     
    # Function call to print the array
    printArray(arr, N)
 
# This code is contributed by rutvik_56


C#
// C# program for the above approach
using System;
class GFG{
     
// Function to print array element
public static void printArray(int []arr, int N)
{   
    // Traverse the array
    for(int i = 0; i < N; i++)
    {
       Console.Write(arr[i] + " ");
    }
}
     
// Function to sort the array in O(N)
public static void sortArray(int []arr, int N)
{
    // Traverse the array
    for(int i = 0; i < N; )
    {
       // If the current element is
       // at correct position
       if (arr[i] == i + 1)
       {
           i++;
       }
        
       // Else swap the current element
       // with it's correct position
       else
       {
           // Swap the value of
           // arr[i] and arr[arr[i]-1]
           int temp1 = arr[i];
           int temp2 = arr[arr[i] - 1];
           arr[i] = temp2;
           arr[temp1 - 1] = temp1;
       }
    }
}
 
// Driver Code   
public static void Main(String[] args)
{
    int []arr = {2, 1, 5, 3, 4};
    int N = arr.Length;
 
    // Function call to sort the array
    sortArray(arr, N);
 
    // Function call to print the array
    printArray(arr, N);
}
}
 
// This code is contributed by shikhasingrajput


输出:
1 2 3 4 5



时间复杂度: O(N) ,其中N是数组的长度。
辅助空间: O(1)