📜  就地算法

📅  最后修改于: 2021-05-08 17:03:22             🧑  作者: Mango

就地有多个定义。一个严格的定义是。

广义的定义

反转数组的非就地实现

C++
// An Not in-place C++ program to reverse an array
#include 
using namespace std;
   
/* Function to reverse arr[] from start to end*/
void revereseArray(int arr[], int n)
{
   // Create a copy array and store reversed
   // elements
   int rev[n];
   for (int i=0; i


Java
// An Not in-place Java program
// to reverse an array
import java.util.*;
  
class GFG
{
    /* Function to reverse arr[]
       from start to end*/
    public static void revereseArray(int []arr, 
                                     int n)
    {
        // Create a copy array
        // and store reversed
        // elements
        int []rev = new int[n];
        for (int i = 0; i < n; i++)
            rev[n - i - 1] = arr[i];
          
        // Now copy reversed 
        // elements back to arr[]
        for (int i = 0; i < n; i++)
            arr[i] = rev[i];
    } 
      
    /* Utility function to
       print an array */
    public static void printArray(int []arr, 
                                  int size)
    {
    for (int i = 0; i < size; i++)
        System.out.print(arr[i] + " ");
    System.out.println("");
    } 
      
    // Driver code
    public static void main(String[] args) 
    {
        int arr[] = {1, 2, 3, 4, 5, 6}; 
        int n = arr.length;
        printArray(arr, n); 
        revereseArray(arr, n); 
        System.out.println("Reversed array is");
        printArray(arr, n); 
    }
}
  
// This code is contributed
// by Harshit Saini


Python3
# An Not in-place Python program 
# to reverse an array
  
''' Function to reverse arr[]
    from start to end '''
def revereseArray(arr, n):
      
    # Create a copy array 
    # and store reversed
    # elements
    rev = n * [0]
    for i in range(0, n):
        rev[n - i - 1] = arr[i]
              
    # Now copy reversed
    # elements back to arr[]
    for i in range(0, n):
        arr[i] = rev[i]
          
# Driver code
if __name__ == "__main__":
    arr = [1, 2, 3, 4, 5, 6]
    n = len(arr)
    print(*arr) 
    revereseArray(arr, n); 
    print("Reversed array is")
    print(*arr) 
      
# This code is contributed
# by Harshit Saini


C#
// An Not in-place C# program
// to reverse an array
using System;
  
class GFG
{
    /* Function to reverse arr[]
    from start to end*/
    public static void revereseArray(int[] arr, 
                                    int n)
    {
        // Create a copy array
        // and store reversed
        // elements
        int[] rev = new int[n];
        for (int i = 0; i < n; i++)
            rev[n - i - 1] = arr[i];
          
        // Now copy reversed 
        // elements back to arr[]
        for (int i = 0; i < n; i++)
            arr[i] = rev[i];
    } 
      
    /* Utility function to
    print an array */
    public static void printArray(int[] arr, 
                                int size)
    {
    for (int i = 0; i < size; i++)
        Console.Write(arr[i] + " ");
    Console.Write("\n");
    } 
      
    // Driver code
    public static void Main() 
    {
        int[] arr = {1, 2, 3, 4, 5, 6}; 
        int n = arr.Length;
        printArray(arr, n); 
        revereseArray(arr, n); 
        Console.WriteLine("Reversed array is");
        printArray(arr, n); 
    }
}
  
// This code is contributed by Ita_c.


C++
// An in-place C++ program to reverse an array
#include 
using namespace std;
   
/* Function to reverse arr[] from start to end*/
void revereseArray(int arr[], int n)
{
   for (int i=0; i


Java
// An in-place Java program
// to reverse an array
import java.util.*;
  
class GFG
{
    public static int __(int x, int y) {return x;}
      
    /* Function to reverse arr[] 
       from start to end*/
    public static void revereseArray(int []arr, 
                                     int n)
    {
        for (int i = 0; i < n / 2; i++)
            arr[i] = __(arr[n - i - 1], 
                        arr[n - i - 1] = arr[i]);
    } 
      
    /* Utility function to 
       print an array */
    public static void printArray(int []arr, 
                                  int size)
    {
        for (int i = 0; i < size; i++)
            System.out.print(Integer.toString(arr[i]) + " "); 
        System.out.println("");
    } 
      
    // Driver code
    public static void main(String[] args) 
    {
        int []arr = new int[]{1, 2, 3, 4, 5, 6}; 
        int n = arr.length;
        printArray(arr, n); 
        revereseArray(arr, n); 
        System.out.println("Reversed array is");
        printArray(arr, n); 
    }
}
  
// This code is contributed 
// by Harshit Saini


Python3
# An in-place Python program
# to reverse an array
  
''' Function to reverse arr[]
    from start to end'''
def revereseArray(arr, n):
      
    for i in range(0, int(n / 2)):
        arr[i], arr[n - i - 1] = arr[n - i - 1], arr[i]
  
  
# Driver code
if __name__ == "__main__":
      
    arr = [1, 2, 3, 4, 5, 6]
    n = len(arr)
    print(*arr)
    revereseArray(arr, n) 
    print("Reversed array is")
    print(*arr)
      
# This code is contributed 
# by Harshit Saini


C#
// An in-place C# program
// to reverse an array
using System; 
      
class GFG
{
    public static int __(int x, int y) {return x;}
      
    /* Function to reverse arr[] 
    from start to end*/
    public static void revereseArray(int []arr, 
                                    int n)
    {
        for (int i = 0; i < n / 2; i++)
            arr[i] = __(arr[n - i - 1], 
                        arr[n - i - 1] = arr[i]);
    } 
      
    /* Utility function to 
    print an array */
    public static void printArray(int []arr, 
                                int size)
    {
        for (int i = 0; i < size; i++)
            Console.Write(arr[i] + " "); 
        Console.WriteLine("");
    } 
      
    // Driver code
    public static void Main(String[] args) 
    {
        int []arr = new int[]{1, 2, 3, 4, 5, 6}; 
        int n = arr.Length;
        printArray(arr, n); 
        revereseArray(arr, n); 
        Console.WriteLine("Reversed array is");
        printArray(arr, n); 
    }
}
  
/* This code is contributed by PrinciRaj1992 */


输出:
1 2 3 4 5 6 
Reversed array is
6 5 4 3 2 1






这需要O(n)额外空间,并且是非就地算法的一个示例。

反转数组的就地实现。

C++

// An in-place C++ program to reverse an array
#include 
using namespace std;
   
/* Function to reverse arr[] from start to end*/
void revereseArray(int arr[], int n)
{
   for (int i=0; i

Java

// An in-place Java program
// to reverse an array
import java.util.*;
  
class GFG
{
    public static int __(int x, int y) {return x;}
      
    /* Function to reverse arr[] 
       from start to end*/
    public static void revereseArray(int []arr, 
                                     int n)
    {
        for (int i = 0; i < n / 2; i++)
            arr[i] = __(arr[n - i - 1], 
                        arr[n - i - 1] = arr[i]);
    } 
      
    /* Utility function to 
       print an array */
    public static void printArray(int []arr, 
                                  int size)
    {
        for (int i = 0; i < size; i++)
            System.out.print(Integer.toString(arr[i]) + " "); 
        System.out.println("");
    } 
      
    // Driver code
    public static void main(String[] args) 
    {
        int []arr = new int[]{1, 2, 3, 4, 5, 6}; 
        int n = arr.length;
        printArray(arr, n); 
        revereseArray(arr, n); 
        System.out.println("Reversed array is");
        printArray(arr, n); 
    }
}
  
// This code is contributed 
// by Harshit Saini

Python3

# An in-place Python program
# to reverse an array
  
''' Function to reverse arr[]
    from start to end'''
def revereseArray(arr, n):
      
    for i in range(0, int(n / 2)):
        arr[i], arr[n - i - 1] = arr[n - i - 1], arr[i]
  
  
# Driver code
if __name__ == "__main__":
      
    arr = [1, 2, 3, 4, 5, 6]
    n = len(arr)
    print(*arr)
    revereseArray(arr, n) 
    print("Reversed array is")
    print(*arr)
      
# This code is contributed 
# by Harshit Saini

C#

// An in-place C# program
// to reverse an array
using System; 
      
class GFG
{
    public static int __(int x, int y) {return x;}
      
    /* Function to reverse arr[] 
    from start to end*/
    public static void revereseArray(int []arr, 
                                    int n)
    {
        for (int i = 0; i < n / 2; i++)
            arr[i] = __(arr[n - i - 1], 
                        arr[n - i - 1] = arr[i]);
    } 
      
    /* Utility function to 
    print an array */
    public static void printArray(int []arr, 
                                int size)
    {
        for (int i = 0; i < size; i++)
            Console.Write(arr[i] + " "); 
        Console.WriteLine("");
    } 
      
    // Driver code
    public static void Main(String[] args) 
    {
        int []arr = new int[]{1, 2, 3, 4, 5, 6}; 
        int n = arr.Length;
        printArray(arr, n); 
        revereseArray(arr, n); 
        Console.WriteLine("Reversed array is");
        printArray(arr, n); 
    }
}
  
/* This code is contributed by PrinciRaj1992 */
输出:
1 2 3 4 5 6 
Reversed array is
6 5 4 3 2 1




这需要O(1)额外的空间来交换元素,这是就地算法的一个示例。

哪些排序算法是就地的,哪些不是?
到位:气泡排序,选择排序,插入排序,堆排序。

不到位:合并排序。请注意,合并排序需要O(n)额外的空间。

那QuickSort呢?为什么称为就地?
QuickSort使用额外的空间进行递归函数调用。根据广泛的定义,它被称为就地,因为所需的额外空间不用于操作输入,而仅用于递归调用。