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

📅  最后修改于: 2021-09-07 03:27:51             🧑  作者: 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


Javascript


输出:
1 2 3 4 5

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live