Phân chia mảng trong python

Cho trước một mảng và một phạm vi, chẳng hạn,
import java.util.*;
import java.io.*;

public class test{
  public static void main(String[] args){
    int arr[] = {2,5,27,56,17,4,9,23,76,1,45};
    int n = arr.length, a=15, b=30;
    int low=0, mid=0, high=n-1, temp;

    while(mid<=high){
      if(arr[mid] < a){ 
            //swap arr[mid] and arr[low] 
            temp = arr[mid]; 
            arr[mid] = arr[low]; 
            arr[low] = temp; 
            low++; 
            mid++; 
        } else if(arr[mid] > b){
            //swap arr[mid] and arr[high]
            temp = arr[mid];
            arr[mid] = arr[high];
            arr[high] = temp;
            high--;
      } else
            mid++;
    }
    System.out.println("The partitioned array is..");
    for(int i=0;i
1, nhiệm vụ là phân chia mảng thành ba phần sao cho,
  • Tất cả các phần tử của mảng nhỏ hơn A xuất hiện trong phân vùng đầu tiên
  • Tất cả các phần tử của mảng nằm trong phạm vi từ A đến B xuất hiện trong phân vùng thứ hai và,
  • Tất cả các phần tử của mảng lớn hơn B đều có trong phân vùng thứ ba

Lưu ý rằng,

import java.util.*;
import java.io.*;

public class test{
  public static void main(String[] args){
    int arr[] = {2,5,27,56,17,4,9,23,76,1,45};
    int n = arr.length, a=15, b=30;
    int low=0, mid=0, high=n-1, temp;

    while(mid<=high){
      if(arr[mid] < a){ 
            //swap arr[mid] and arr[low] 
            temp = arr[mid]; 
            arr[mid] = arr[low]; 
            arr[low] = temp; 
            low++; 
            mid++; 
        } else if(arr[mid] > b){
            //swap arr[mid] and arr[high]
            temp = arr[mid];
            arr[mid] = arr[high];
            arr[high] = temp;
            high--;
      } else
            mid++;
    }
    System.out.println("The partitioned array is..");
    for(int i=0;i
2 và, các phần tử riêng lẻ trong mỗi phân vùng không nhất thiết phải được sắp xếp

Điều này nghe có giống với sắp xếp nhanh không?

Vâng, đúng vậy. Điều này rất giống với bước phân vùng sắp xếp nhanh. Trong cách sắp xếp nhanh, chúng tôi chọn một phần tử trục trong một mảng và đặt trục vào chỉ mục bên phải sao cho tất cả các phần tử nhỏ hơn trục đều ở bên trái của nó và tất cả các phần tử lớn hơn trục sẽ ở bên phải của nó. Điều này có thể được so sánh với phân vùng ba chiều, trong đó trục không chỉ là một phần tử mà còn là một phạm vi

import java.util.*;
import java.io.*;

public class test{
  public static void main(String[] args){
    int arr[] = {2,5,27,56,17,4,9,23,76,1,45};
    int n = arr.length, a=15, b=30;
    int low=0, mid=0, high=n-1, temp;

    while(mid<=high){
      if(arr[mid] < a){ 
            //swap arr[mid] and arr[low] 
            temp = arr[mid]; 
            arr[mid] = arr[low]; 
            arr[low] = temp; 
            low++; 
            mid++; 
        } else if(arr[mid] > b){
            //swap arr[mid] and arr[high]
            temp = arr[mid];
            arr[mid] = arr[high];
            arr[high] = temp;
            high--;
      } else
            mid++;
    }
    System.out.println("The partitioned array is..");
    for(int i=0;i
1

Bây giờ, chúng ta hãy xem làm thế nào vấn đề này có thể được thực hiện

Cách tiếp cận ngây thơ

Điều này có thể được giải quyết bằng cách sắp xếp mảng. Nhưng, điều đó sẽ mất thứ tự của

import java.util.*;
import java.io.*;

public class test{
  public static void main(String[] args){
    int arr[] = {2,5,27,56,17,4,9,23,76,1,45};
    int n = arr.length, a=15, b=30;
    int low=0, mid=0, high=n-1, temp;

    while(mid<=high){
      if(arr[mid] < a){ 
            //swap arr[mid] and arr[low] 
            temp = arr[mid]; 
            arr[mid] = arr[low]; 
            arr[low] = temp; 
            low++; 
            mid++; 
        } else if(arr[mid] > b){
            //swap arr[mid] and arr[high]
            temp = arr[mid];
            arr[mid] = arr[high];
            arr[high] = temp;
            high--;
      } else
            mid++;
    }
    System.out.println("The partitioned array is..");
    for(int i=0;i
4 thời gian. (trong đó n là kích thước của mảng). Chúng ta có thể nghĩ ra một giải pháp hiệu quả hơn trong một lượt mà không thực sự sắp xếp mảng không?

Phương pháp tiếp cận hiệu quả

  • Lấy ba con trỏ
    import java.util.*;
    import java.io.*;
    
    public class test{
      public static void main(String[] args){
        int arr[] = {2,5,27,56,17,4,9,23,76,1,45};
        int n = arr.length, a=15, b=30;
        int low=0, mid=0, high=n-1, temp;
    
        while(mid<=high){
          if(arr[mid] < a){ 
                //swap arr[mid] and arr[low] 
                temp = arr[mid]; 
                arr[mid] = arr[low]; 
                arr[low] = temp; 
                low++; 
                mid++; 
            } else if(arr[mid] > b){
                //swap arr[mid] and arr[high]
                temp = arr[mid];
                arr[mid] = arr[high];
                arr[high] = temp;
                high--;
          } else
                mid++;
        }
        System.out.println("The partitioned array is..");
        for(int i=0;i
    5,
    import java.util.*;
    import java.io.*;
    
    public class test{
      public static void main(String[] args){
        int arr[] = {2,5,27,56,17,4,9,23,76,1,45};
        int n = arr.length, a=15, b=30;
        int low=0, mid=0, high=n-1, temp;
    
        while(mid<=high){
          if(arr[mid] < a){ 
                //swap arr[mid] and arr[low] 
                temp = arr[mid]; 
                arr[mid] = arr[low]; 
                arr[low] = temp; 
                low++; 
                mid++; 
            } else if(arr[mid] > b){
                //swap arr[mid] and arr[high]
                temp = arr[mid];
                arr[mid] = arr[high];
                arr[high] = temp;
                high--;
          } else
                mid++;
        }
        System.out.println("The partitioned array is..");
        for(int i=0;i
    6 và
    import java.util.*;
    import java.io.*;
    
    public class test{
      public static void main(String[] args){
        int arr[] = {2,5,27,56,17,4,9,23,76,1,45};
        int n = arr.length, a=15, b=30;
        int low=0, mid=0, high=n-1, temp;
    
        while(mid<=high){
          if(arr[mid] < a){ 
                //swap arr[mid] and arr[low] 
                temp = arr[mid]; 
                arr[mid] = arr[low]; 
                arr[low] = temp; 
                low++; 
                mid++; 
            } else if(arr[mid] > b){
                //swap arr[mid] and arr[high]
                temp = arr[mid];
                arr[mid] = arr[high];
                arr[high] = temp;
                high--;
          } else
                mid++;
        }
        System.out.println("The partitioned array is..");
        for(int i=0;i
    7
  • Đặt
    import java.util.*;
    import java.io.*;
    
    public class test{
      public static void main(String[] args){
        int arr[] = {2,5,27,56,17,4,9,23,76,1,45};
        int n = arr.length, a=15, b=30;
        int low=0, mid=0, high=n-1, temp;
    
        while(mid<=high){
          if(arr[mid] < a){ 
                //swap arr[mid] and arr[low] 
                temp = arr[mid]; 
                arr[mid] = arr[low]; 
                arr[low] = temp; 
                low++; 
                mid++; 
            } else if(arr[mid] > b){
                //swap arr[mid] and arr[high]
                temp = arr[mid];
                arr[mid] = arr[high];
                arr[high] = temp;
                high--;
          } else
                mid++;
        }
        System.out.println("The partitioned array is..");
        for(int i=0;i
    8 thành chỉ mục thứ 0 và
    import java.util.*;
    import java.io.*;
    
    public class test{
      public static void main(String[] args){
        int arr[] = {2,5,27,56,17,4,9,23,76,1,45};
        int n = arr.length, a=15, b=30;
        int low=0, mid=0, high=n-1, temp;
    
        while(mid<=high){
          if(arr[mid] < a){ 
                //swap arr[mid] and arr[low] 
                temp = arr[mid]; 
                arr[mid] = arr[low]; 
                arr[low] = temp; 
                low++; 
                mid++; 
            } else if(arr[mid] > b){
                //swap arr[mid] and arr[high]
                temp = arr[mid];
                arr[mid] = arr[high];
                arr[high] = temp;
                high--;
          } else
                mid++;
        }
        System.out.println("The partitioned array is..");
        for(int i=0;i
    7 thành chỉ mục
    import java.util.*;
    import java.io.*;
    
    public class test{
      public static void main(String[] args){
        int arr[] = {2,5,27,56,17,4,9,23,76,1,45};
        int n = arr.length, a=15, b=30;
        int low=0, mid=0, high=n-1, temp;
    
        while(mid<=high){
          if(arr[mid] < a){ 
                //swap arr[mid] and arr[low] 
                temp = arr[mid]; 
                arr[mid] = arr[low]; 
                arr[low] = temp; 
                low++; 
                mid++; 
            } else if(arr[mid] > b){
                //swap arr[mid] and arr[high]
                temp = arr[mid];
                arr[mid] = arr[high];
                arr[high] = temp;
                high--;
          } else
                mid++;
        }
        System.out.println("The partitioned array is..");
        for(int i=0;i
    1 ban đầu
  • Giả sử, phân vùng chứa tất cả các phần tử nhỏ hơn A là phân vùng đầu tiên và khối chứa tất cả các phần tử lớn hơn B là phân vùng thứ ba
  • Sử dụng “thấp” để duy trì ranh giới của phân vùng đầu tiên và “cao” để duy trì ranh giới của phân vùng thứ ba
  • Trong khi, “giữa” lặp lại mảng và hoán đổi các phần tử phù hợp với phân vùng thứ nhất và thứ ba
  • Mảng kết quả được chia thành 3 phân vùng

Thực hiện

  • Khởi tạo
    import java.util.*;
    import java.io.*;
    
    public class test{
      public static void main(String[] args){
        int arr[] = {2,5,27,56,17,4,9,23,76,1,45};
        int n = arr.length, a=15, b=30;
        int low=0, mid=0, high=n-1, temp;
    
        while(mid<=high){
          if(arr[mid] < a){ 
                //swap arr[mid] and arr[low] 
                temp = arr[mid]; 
                arr[mid] = arr[low]; 
                arr[low] = temp; 
                low++; 
                mid++; 
            } else if(arr[mid] > b){
                //swap arr[mid] and arr[high]
                temp = arr[mid];
                arr[mid] = arr[high];
                arr[high] = temp;
                high--;
          } else
                mid++;
        }
        System.out.println("The partitioned array is..");
        for(int i=0;i
    2,
    import java.util.*;
    import java.io.*;
    
    public class test{
      public static void main(String[] args){
        int arr[] = {2,5,27,56,17,4,9,23,76,1,45};
        int n = arr.length, a=15, b=30;
        int low=0, mid=0, high=n-1, temp;
    
        while(mid<=high){
          if(arr[mid] < a){ 
                //swap arr[mid] and arr[low] 
                temp = arr[mid]; 
                arr[mid] = arr[low]; 
                arr[low] = temp; 
                low++; 
                mid++; 
            } else if(arr[mid] > b){
                //swap arr[mid] and arr[high]
                temp = arr[mid];
                arr[mid] = arr[high];
                arr[high] = temp;
                high--;
          } else
                mid++;
        }
        System.out.println("The partitioned array is..");
        for(int i=0;i
    3 và
    import java.util.*;
    import java.io.*;
    
    public class test{
      public static void main(String[] args){
        int arr[] = {2,5,27,56,17,4,9,23,76,1,45};
        int n = arr.length, a=15, b=30;
        int low=0, mid=0, high=n-1, temp;
    
        while(mid<=high){
          if(arr[mid] < a){ 
                //swap arr[mid] and arr[low] 
                temp = arr[mid]; 
                arr[mid] = arr[low]; 
                arr[low] = temp; 
                low++; 
                mid++; 
            } else if(arr[mid] > b){
                //swap arr[mid] and arr[high]
                temp = arr[mid];
                arr[mid] = arr[high];
                arr[high] = temp;
                high--;
          } else
                mid++;
        }
        System.out.println("The partitioned array is..");
        for(int i=0;i
    4
  • Sử dụng mid để lặp lại mảng và truy cập từng phần tử. Tại một yếu tố
    import java.util.*;
    import java.io.*;
    
    public class test{
      public static void main(String[] args){
        int arr[] = {2,5,27,56,17,4,9,23,76,1,45};
        int n = arr.length, a=15, b=30;
        int low=0, mid=0, high=n-1, temp;
    
        while(mid<=high){
          if(arr[mid] < a){ 
                //swap arr[mid] and arr[low] 
                temp = arr[mid]; 
                arr[mid] = arr[low]; 
                arr[low] = temp; 
                low++; 
                mid++; 
            } else if(arr[mid] > b){
                //swap arr[mid] and arr[high]
                temp = arr[mid];
                arr[mid] = arr[high];
                arr[high] = temp;
                high--;
          } else
                mid++;
        }
        System.out.println("The partitioned array is..");
        for(int i=0;i
    5,
    • import java.util.*;
      import java.io.*;
      
      public class test{
        public static void main(String[] args){
          int arr[] = {2,5,27,56,17,4,9,23,76,1,45};
          int n = arr.length, a=15, b=30;
          int low=0, mid=0, high=n-1, temp;
      
          while(mid<=high){
            if(arr[mid] < a){ 
                  //swap arr[mid] and arr[low] 
                  temp = arr[mid]; 
                  arr[mid] = arr[low]; 
                  arr[low] = temp; 
                  low++; 
                  mid++; 
              } else if(arr[mid] > b){
                  //swap arr[mid] and arr[high]
                  temp = arr[mid];
                  arr[mid] = arr[high];
                  arr[high] = temp;
                  high--;
            } else
                  mid++;
          }
          System.out.println("The partitioned array is..");
          for(int i=0;i
      6 nhỏ hơn A, sau đó, hoán đổi nó với
      import java.util.*;
      import java.io.*;
      
      public class test{
        public static void main(String[] args){
          int arr[] = {2,5,27,56,17,4,9,23,76,1,45};
          int n = arr.length, a=15, b=30;
          int low=0, mid=0, high=n-1, temp;
      
          while(mid<=high){
            if(arr[mid] < a){ 
                  //swap arr[mid] and arr[low] 
                  temp = arr[mid]; 
                  arr[mid] = arr[low]; 
                  arr[low] = temp; 
                  low++; 
                  mid++; 
              } else if(arr[mid] > b){
                  //swap arr[mid] and arr[high]
                  temp = arr[mid];
                  arr[mid] = arr[high];
                  arr[high] = temp;
                  high--;
            } else
                  mid++;
          }
          System.out.println("The partitioned array is..");
          for(int i=0;i
      7 và tăng cả thấp và trung bình lên một
    • import java.util.*;
      import java.io.*;
      
      public class test{
        public static void main(String[] args){
          int arr[] = {2,5,27,56,17,4,9,23,76,1,45};
          int n = arr.length, a=15, b=30;
          int low=0, mid=0, high=n-1, temp;
      
          while(mid<=high){
            if(arr[mid] < a){ 
                  //swap arr[mid] and arr[low] 
                  temp = arr[mid]; 
                  arr[mid] = arr[low]; 
                  arr[low] = temp; 
                  low++; 
                  mid++; 
              } else if(arr[mid] > b){
                  //swap arr[mid] and arr[high]
                  temp = arr[mid];
                  arr[mid] = arr[high];
                  arr[high] = temp;
                  high--;
            } else
                  mid++;
          }
          System.out.println("The partitioned array is..");
          for(int i=0;i
      6 lớn hơn B, hoán đổi nó với
      import java.util.*;
      import java.io.*;
      
      public class test{
        public static void main(String[] args){
          int arr[] = {2,5,27,56,17,4,9,23,76,1,45};
          int n = arr.length, a=15, b=30;
          int low=0, mid=0, high=n-1, temp;
      
          while(mid<=high){
            if(arr[mid] < a){ 
                  //swap arr[mid] and arr[low] 
                  temp = arr[mid]; 
                  arr[mid] = arr[low]; 
                  arr[low] = temp; 
                  low++; 
                  mid++; 
              } else if(arr[mid] > b){
                  //swap arr[mid] and arr[high]
                  temp = arr[mid];
                  arr[mid] = arr[high];
                  arr[high] = temp;
                  high--;
            } else
                  mid++;
          }
          System.out.println("The partitioned array is..");
          for(int i=0;i
      9 và giảm mức cao xuống một
    • Nếu không, hãy tăng
      import java.util.*;
      import java.io.*;
      
      public class test{
        public static void main(String[] args){
          int arr[] = {2,5,27,56,17,4,9,23,76,1,45};
          int n = arr.length, a=15, b=30;
          int low=0, mid=0, high=n-1, temp;
      
          while(mid<=high){
            if(arr[mid] < a){ 
                  //swap arr[mid] and arr[low] 
                  temp = arr[mid]; 
                  arr[mid] = arr[low]; 
                  arr[low] = temp; 
                  low++; 
                  mid++; 
              } else if(arr[mid] > b){
                  //swap arr[mid] and arr[high]
                  temp = arr[mid];
                  arr[mid] = arr[high];
                  arr[high] = temp;
                  high--;
            } else
                  mid++;
          }
          System.out.println("The partitioned array is..");
          for(int i=0;i
      6 lên một
  • Mảng kết quả là mảng được phân vùng

Ví dụ

def threeWayPartition(array, a, b):
    low=0
    mid=0
    high=len(array)-1
    
    while(mid <= high):
        if(array[mid] < a): #swap array[mid] with array[low] 
            temp = array[mid] 
            array[mid] = array[low] 
            array[low] = temp 
            low += 1 
            mid += 1 
        elif(array[mid] > b):
            #swap array[mid] with array[high]
            temp = array[mid]
            array[mid] = array[high]
            array[high] = temp
            high -= 1
        else:
            mid += 1
    return array

if __name__ == "__main__":
  print("Enter the array elements seperated by spaces: ")
  str_arr = input().split(' ')
  arr = [int(num) for num in str_arr]
  a, b = [int(x) for x in input("Enter the range [A,B] seperated by spaces (NOTE: A <= B) ").split()]
  arr = threeWayPartition(arr, a, b)
  print("The array after three way partitioning is ", arr)

đầu ra

Phân chia mảng trong python

import java.util.*;
import java.io.*;

public class test{
  public static void main(String[] args){
    int arr[] = {2,5,27,56,17,4,9,23,76,1,45};
    int n = arr.length, a=15, b=30;
    int low=0, mid=0, high=n-1, temp;

    while(mid<=high){
      if(arr[mid] < a){ 
            //swap arr[mid] and arr[low] 
            temp = arr[mid]; 
            arr[mid] = arr[low]; 
            arr[low] = temp; 
            low++; 
            mid++; 
        } else if(arr[mid] > b){
            //swap arr[mid] and arr[high]
            temp = arr[mid];
            arr[mid] = arr[high];
            arr[high] = temp;
            high--;
      } else
            mid++;
    }
    System.out.println("The partitioned array is..");
    for(int i=0;i

đầu ra

Phân chia mảng trong python

Thời gian và không gian phức tạp

Độ phức tạp thời gian của thuật toán này theo thứ tự n, i. e. ,

import java.util.*;
import java.io.*;

public class test{
  public static void main(String[] args){
    int arr[] = {2,5,27,56,17,4,9,23,76,1,45};
    int n = arr.length, a=15, b=30;
    int low=0, mid=0, high=n-1, temp;

    while(mid<=high){
      if(arr[mid] < a){ 
            //swap arr[mid] and arr[low] 
            temp = arr[mid]; 
            arr[mid] = arr[low]; 
            arr[low] = temp; 
            low++; 
            mid++; 
        } else if(arr[mid] > b){
            //swap arr[mid] and arr[high]
            temp = arr[mid];
            arr[mid] = arr[high];
            arr[high] = temp;
            high--;
      } else
            mid++;
    }
    System.out.println("The partitioned array is..");
    for(int i=0;i
11 vì đây là thuật toán vượt qua một lần. Thuật toán tại chỗ và không tốn thêm bất kỳ dung lượng nào, làm cho độ phức tạp của không gian không đổi i. e. ,
import java.util.*;
import java.io.*;

public class test{
  public static void main(String[] args){
    int arr[] = {2,5,27,56,17,4,9,23,76,1,45};
    int n = arr.length, a=15, b=30;
    int low=0, mid=0, high=n-1, temp;

    while(mid<=high){
      if(arr[mid] < a){ 
            //swap arr[mid] and arr[low] 
            temp = arr[mid]; 
            arr[mid] = arr[low]; 
            arr[low] = temp; 
            low++; 
            mid++; 
        } else if(arr[mid] > b){
            //swap arr[mid] and arr[high]
            temp = arr[mid];
            arr[mid] = arr[high];
            arr[high] = temp;
            high--;
      } else
            mid++;
    }
    System.out.println("The partitioned array is..");
    for(int i=0;i
12

Các ứng dụng

O một trong những ứng dụng quan trọng của thuật toán này là sắp xếp một mảng có ba loại phần tử trong đó n>=3. S à, chúng ta có một mảng gồm 0, 1 và 2. Sau đó, phương pháp này có thể được sử dụng để sắp xếp nó trong một lần với độ phức tạp không gian không đổi. Trong trường hợp này, A=1 và B=1.

Phân vùng () được sử dụng để làm gì trong Python?

Định nghĩa và cách sử dụng. Phương thức partition() tìm kiếm một chuỗi đã chỉ định và chia chuỗi thành một bộ chứa ba phần tử . Phần tử đầu tiên chứa phần trước chuỗi đã chỉ định. Phần tử thứ hai chứa chuỗi đã chỉ định.

Phân vùng một mảng có nghĩa là gì?

Định nghĩa. (1) Việc chia một tập hợp thành các tập hợp khác nhau không rỗng bao phủ hoàn toàn tập hợp . (2) Để sắp xếp lại các phần tử của một mảng thành hai (hoặc nhiều) nhóm, thông thường, sao cho các phần tử trong nhóm đầu tiên nhỏ hơn một giá trị và các phần tử trong nhóm thứ hai lớn hơn.

Làm cách nào để tách danh sách trong Python?

Cách tách các phần tử của danh sách trong Python. .
Sử dụng khả năng hiểu danh sách để lặp lại danh sách
Trên mỗi lần lặp, gọi phương thức split() để tách từng chuỗi
Trả lại phần của mỗi chuỗi bạn muốn giữ lại

Python có phải là một phân vùng không?

Phân vùng () trong Python là gì? . Nó trả về một bộ chứa phần trước dấu phân cách, dấu phân cách và phần sau dấu phân cách. splits the string at the first occurrence of the separator. It returns a tuple containing the part before the separator, the separator, and the part after the separator.