<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 對 0、1 和 2 的數組進行排序 > 原文: [https://www.geeksforgeeks.org/sort-an-array-of-0s-1s-and-2s/](https://www.geeksforgeeks.org/sort-an-array-of-0s-1s-and-2s/) 給定一個數組 **A []** ,其中包含 0、1 和 2。 任務是編寫一個對給定數組進行排序的函數。 函數應將全 0 放在最前面,然后將全 1 放在最后,并將全 2 放在最后。 **示例**: ``` Input: {0, 1, 2, 0, 1, 2} Output: {0, 0, 1, 1, 2, 2} Input: {0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1} Output: {0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2} ``` 在這篇文章([對 0、1、2 和 2s 的數組進行排序(簡單計數)](https://www.geeksforgeeks.org/sort-array-0s-1s-2s-simple-counting/))中討論了一種簡單的解決方案。 **方法 1** * **Approach:**The problem is similar to our old post [Segregate 0s and 1s in an array](https://www.geeksforgeeks.org/segregate-0s-and-1s-in-an-array-by-traversing-array-once/), and both of these problems are variation of famous [Dutch national flag problem](http://www.csse.monash.edu.au/~lloyd/tildeAlgDS/Sort/Flag/). *問題是由三種顏色構成的,這里是'0','1'和'2'。 數組分為四個部分:* 1. a [1..Lo-1]零(紅色) 2. a [Lo..Mid-1]個(白色) 3. [Mid..Hi]未知 4. a [Hi + 1..N]兩位(藍色) **荷蘭國旗算法或 3 路分區**: 首先,整個數組是未知的。 有三個指標– **低,中和高**。 最初,低=中= 1,高=N。 * 如果第 ith 個元素為 0,則將該元素交換到較低范圍,從而縮小未知范圍。 * 同樣,如果元素為 1,則保持原樣,但縮小未知范圍。 * 如果元素為 2,則將其與大范圍的元素交換。 * **算法**: 1. 保持三個索引為低= 1,中= 1 和高= N,并且有四個范圍,從 1 到低(包含 0 的范圍),低到中(包含 1 的范圍),中到高(包含未知元素的范圍) 和高到 N(包含 2 的范圍)。 2. 從頭到尾遍歷數組,中間值不高。 (循環計數器是 i) 3. 如果元素為 0,則將其與索引為低的元素交換,并更新 low = low + 1 和 mid = mid + 1 4. 如果元素為 1,則更新 mid = mid + 1 5. 如果元素為 2,則將其與索引為高的元素交換,并更新 high = high – 1 并更新 i = i –1。因為未處理交換的元素 6. 打印輸出數組。 * **Dry Run:** Part way through the process, some red, white and blue elements are known and are in the “right” place. The section of unknown elements, a[Mid..Hi], is shrunk by examining a[Mid]: > ![DNF1](https://img.kancloud.cn/c8/ce/c8ce7d999fcbc429b1339dab1e4fa677_469x112.png) > > 檢查一個[中]。 存在三種可能性: > 和[Mid]是(0)紅色,(1)白色或(2)藍色。 > 情況(0)a [Mid]為紅色,交換 a [Lo]和 a [Mid]; Lo ++; 中++ > > ![DNF2](https://img.kancloud.cn/ac/65/ac659abc93edf73e453f504ff8b5254b_496x99.png) > > 情況(1)a [中]是白色,中++ > > ![DNF3](https://img.kancloud.cn/ce/af/ceaf1a07fe2c1028a07198c3fb056297_471x131.png) > > 情況(2)a [Mid]為藍色,交換 a [Mid]和 a [Hi]; 嗨– > > ![DNF4](https://img.kancloud.cn/54/6a/546ac68b278e7d45f5846ec72d7f5a7d_467x141.png) > > 繼續直到>中。 * **實現**: ## C++ ``` // C++ program to sort an array // with 0, 1 and 2 in a single pass #include <bits/stdc++.h> using namespace std; // Function to sort the input array, // the array is assumed // to have values in {0, 1, 2} void sort012(int a[], int arr_size) { ????int lo = 0; ????int hi = arr_size - 1; ????int mid = 0; ????// Iterate till all the elements ????// are sorted ????while (mid <= hi) { ????????switch (a[mid]) { ????????// If the element is 0 ????????case 0: ????????????swap(a[lo++], a[mid++]); ????????????break; ????????// If the element is 1 . ????????case 1: ????????????mid++; ????????????break; ????????// If the element is 2 ????????case 2: ????????????swap(a[mid], a[hi--]); ????????????break; ????????} ????} } // Function to print array arr[] void printArray(int arr[], int arr_size) { ????// Iterate and print every element ????for (int i = 0; i < arr_size; i++) ????????cout << arr[i] << " "; } // Driver Code int main() { ????int arr[] = { 0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1 }; ????int n = sizeof(arr) / sizeof(arr[0]); ????sort012(arr, n); ????cout << "array after segregation "; ????printArray(arr, n); ????return 0; } // This code is contributed by Shivi_Aggarwal ``` ## C ``` // C program to sort an array with 0, 1 and 2 // in a single pass #include <stdio.h> /* Function to swap *a and *b */ void swap(int* a, int* b); // Sort the input array, the array is assumed to // have values in {0, 1, 2} void sort012(int a[], int arr_size) { ????int lo = 0; ????int hi = arr_size - 1; ????int mid = 0; ????while (mid <= hi) { ????????switch (a[mid]) { ????????case 0: ????????????swap(&a[lo++], &a[mid++]); ????????????break; ????????case 1: ????????????mid++; ????????????break; ????????case 2: ????????????swap(&a[mid], &a[hi--]); ????????????break; ????????} ????} } /* UTILITY FUNCTIONS */ void swap(int* a, int* b) { ????int temp = *a; ????*a = *b; ????*b = temp; } /* Utility function to print array arr[] */ void printArray(int arr[], int arr_size) { ????int i; ????for (i = 0; i < arr_size; i++) ????????printf("%d ", arr[i]); ????printf("n"); } /* driver program to test */ int main() { ????int arr[] = { 0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1 }; ????int arr_size = sizeof(arr) / sizeof(arr[0]); ????int i; ????sort012(arr, arr_size); ????printf("array after segregation "); ????printArray(arr, arr_size); ????getchar(); ????return 0; } ``` ## Java ``` // Java program to sort an array of 0, 1 and 2 import java.io.*; class countzot { ????// Sort the input array, the array is assumed to ????// have values in {0, 1, 2} ????static void sort012(int a[], int arr_size) ????{ ????????int lo = 0; ????????int hi = arr_size - 1; ????????int mid = 0, temp = 0; ????????while (mid <= hi) { ????????????switch (a[mid]) { ????????????case 0: { ????????????????temp = a[lo]; ????????????????a[lo] = a[mid]; ????????????????a[mid] = temp; ????????????????lo++; ????????????????mid++; ????????????????break; ????????????} ????????????case 1: ????????????????mid++; ????????????????break; ????????????case 2: { ????????????????temp = a[mid]; ????????????????a[mid] = a[hi]; ????????????????a[hi] = temp; ????????????????hi--; ????????????????break; ????????????} ????????????} ????????} ????} ????/* Utility function to print array arr[] */ ????static void printArray(int arr[], int arr_size) ????{ ????????int i; ????????for (i = 0; i < arr_size; i++) ????????????System.out.print(arr[i] + " "); ????????System.out.println(""); ????} ????/*Driver function to check for above functions*/ ????public static void main(String[] args) ????{ ????????int arr[] = { 0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1 }; ????????int arr_size = arr.length; ????????sort012(arr, arr_size); ????????System.out.println("Array after seggregation "); ????????printArray(arr, arr_size); ????} } /*This code is contributed by Devesh Agrawal*/ ``` ## 蛇蛇 ``` # Python program to sort an array with? # 0, 1 and 2 in a single pass # Function to sort array def sort012( a, arr_size): ????lo = 0 ????hi = arr_size - 1 ????mid = 0 ????while mid <= hi: ????????if a[mid] == 0: ????????????a[lo], a[mid] = a[mid], a[lo] ????????????lo = lo + 1 ????????????mid = mid + 1 ????????elif a[mid] == 1: ????????????mid = mid + 1 ????????else: ????????????a[mid], a[hi] = a[hi], a[mid]? ????????????hi = hi - 1 ????return a # Function to print array def printArray( a): ????for k in a: ????????print k, # Driver Program arr = [0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1] arr_size = len(arr) arr = sort012( arr, arr_size) print "Array after segregation :\n", printArray(arr) # Contributed by Harshit Agrawal ``` ## C# ``` // C# program to sort an // array of 0, 1 and 2 using System; class GFG { ????// Sort the input array, the array is assumed to ????// have values in {0, 1, 2} ????static void sort012(int[] a, int arr_size) ????{ ????????int lo = 0; ????????int hi = arr_size - 1; ????????int mid = 0, temp = 0; ????????while (mid <= hi) { ????????????switch (a[mid]) { ????????????case 0: { ????????????????temp = a[lo]; ????????????????a[lo] = a[mid]; ????????????????a[mid] = temp; ????????????????lo++; ????????????????mid++; ????????????????break; ????????????} ????????????case 1: ????????????????mid++; ????????????????break; ????????????case 2: { ????????????????temp = a[mid]; ????????????????a[mid] = a[hi]; ????????????????a[hi] = temp; ????????????????hi--; ????????????????break; ????????????} ????????????} ????????} ????} ????/* Utility function to print array arr[] */ ????static void printArray(int[] arr, int arr_size) ????{ ????????int i; ????????for (i = 0; i < arr_size; i++) ????????????Console.Write(arr[i] + " "); ????????Console.WriteLine(""); ????} ????/*Driver function to check for above functions*/ ????public static void Main() ????{ ????????int[] arr = { 0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1 }; ????????int arr_size = arr.Length; ????????sort012(arr, arr_size); ????????Console.Write("Array after seggregation "); ????????printArray(arr, arr_size); ????} } // This code is contributed by Sam007 ``` ## PHP ``` <?php? // PHP program to sort an array // with 0, 1 and 2 in a single pass // Sort the input array, the array is? // assumed to have values in {0, 1, 2} function sort012(&$a, $arr_size) { ????$lo = 0; ????$hi = $arr_size - 1; ????$mid = 0; ????while ($mid <= $hi) ????{ ????????switch ($a[$mid]) ????????{ ????????case 0: ????????????swap($a[$lo++], $a[$mid++]); ????????????break; ????????case 1: ????????????$mid++; ????????????break; ????????case 2: ????????????swap($a[$mid], $a[$hi--]); ????????????break; ????????} ????} } /* UTILITY FUNCTIONS */ function swap(&$a, &$b) { ????$temp = $a; ????$a = $b; ????$b = $temp; } /* Utility function to print array arr[] */ function printArray(&$arr, $arr_size) { ????for ($i = 0; $i < $arr_size; $i++) ????????echo $arr[$i]." "; ????echo "\n"; } // Driver Code $arr = array(0, 1, 1, 0, 1, 2,? ?????????????1, 2, 0, 0, 0, 1); $arr_size = sizeof($arr); sort012($arr, $arr_size); echo "array after segregation "; printArray($arr, $arr_size); // This code is contributed // by ChitraNayal ?> ``` **輸出**: ``` array after segregation 0 0 0 0 0 1 1 1 1 1 2 2 ``` * **復雜度分析**: * **時間復雜度**:`O(n)`。 僅需要遍歷數組一次。 * **空間復雜度**:`O(1)`。 不需要多余的空間。 上面的代碼對某些實際上不需要的輸入執行不必要的交換。 可以對其進行修改以減少一些交換。 *感謝 Ankur Roy 建議進行此優化。* **方法 2** * **方法**:計算給定數組中的 0、1 和 2 的數量。 然后在開始時存儲所有 0,然后存儲所有 1,然后存儲所有 2。 * **算法**: 1. 保持三個計數器 c0 計數 0s,c1 計數 1s,c2 計數 2s 2. 遍歷數組并增加 c0 的數量為 0,增加 c1 的數量為 1,增加 c2 的數量為 2 3. 現在再次遍歷數組,將第一個 c0 元素替換為 0,將下一個 c1 元素替換為 1,將下一個 c2 元素替換為 2。 * **實現**: ## C++ ``` // C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Utility function to print the contents of an array void printArr(int arr[], int n) { ????for (int i = 0; i < n; i++) ????????cout << arr[i] << " "; } // Function to sort the array of 0s, 1s and 2s void sortArr(int arr[], int n) { ????int i, cnt0 = 0, cnt1 = 0, cnt2 = 0; ????// Count the number of 0s, 1s and 2s in the array ????for (i = 0; i < n; i++) { ????????switch (arr[i]) { ????????case 0: ????????????cnt0++; ????????????break; ????????case 1: ????????????cnt1++; ????????????break; ????????case 2: ????????????cnt2++; ????????????break; ????????} ????} ????// Update the array ????i = 0; ????// Store all the 0s in the beginning ????while (cnt0 > 0) { ????????arr[i++] = 0; ????????cnt0--; ????} ????// Then all the 1s ????while (cnt1 > 0) { ????????arr[i++] = 1; ????????cnt1--; ????} ????// Finally all the 2s ????while (cnt2 > 0) { ????????arr[i++] = 2; ????????cnt2--; ????} ????// Print the sorted array ????printArr(arr, n); } // Driver code int main() { ????int arr[] = { 0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1 }; ????int n = sizeof(arr) / sizeof(int); ????sortArr(arr, n); ????return 0; } ``` ## Java ``` // Java implementation of the approach import java.io.*; class GFG { ????// Utility function to print the contents of an array ????static void printArr(int arr[], int n) ????{ ????????for (int i = 0; i < n; i++) ????????????System.out.print(arr[i] + " "); ????} ????// Function to sort the array of 0s, 1s and 2s ????static void sortArr(int arr[], int n) ????{ ????????int i, cnt0 = 0, cnt1 = 0, cnt2 = 0; ????????// Count the number of 0s, 1s and 2s in the array ????????for (i = 0; i < n; i++) { ????????????switch (arr[i]) { ????????????case 0: ????????????????cnt0++; ????????????????break; ????????????case 1: ????????????????cnt1++; ????????????????break; ????????????case 2: ????????????????cnt2++; ????????????????break; ????????????} ????????} ????????// Update the array ????????i = 0; ????????// Store all the 0s in the beginning ????????while (cnt0 > 0) { ????????????arr[i++] = 0; ????????????cnt0--; ????????} ????????// Then all the 1s ????????while (cnt1 > 0) { ????????????arr[i++] = 1; ????????????cnt1--; ????????} ????????// Finally all the 2s ????????while (cnt2 > 0) { ????????????arr[i++] = 2; ????????????cnt2--; ????????} ????????// Print the sorted array ????????printArr(arr, n); ????} ????// Driver code ????public static void main(String[] args) ????{ ????????int arr[] = { 0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1 }; ????????int n = arr.length; ????????sortArr(arr, n); ????} } // This code is contributed by shubhamsingh10 ``` ## Python ``` # Python implementation of the approach # Utility function to print contents of an array def printArr(arr, n): ????for i in range(n): ????????print(arr[i],end=" ") # Function to sort the array of 0s, 1s and 2s def sortArr(arr, n): ????cnt0 = 0 ????cnt1 = 0 ????cnt2 = 0 ????# Count the number of 0s, 1s and 2s in the array ????for i in range(n): ????????if arr[i] == 0: ????????????cnt0+=1 ????????elif arr[i] == 1: ????????????cnt1+=1 ????????elif arr[i] == 2: ????????????cnt2+=1 ????# Update the array ????i = 0 ????# Store all the 0s in the beginning ????while (cnt0 > 0): ????????arr[i] = 0 ????????i+=1 ????????cnt0-=1 ????# Then all the 1s ????while (cnt1 > 0): ????????arr[i] = 1 ????????i+=1 ????????cnt1-=1 ????# Finally all the 2s ????while (cnt2 > 0): ????????arr[i] = 2 ????????i+=1 ????????cnt2-=1 ????# Prthe sorted array ????printArr(arr, n) # Driver code arr = [0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1] n = len(arr) sortArr(arr, n) #This code is contributed by shubhamsingh10 ``` ## C# ``` // C# implementation of the approach using System; class GFG { ????// Utility function to print the contents of an array ????static void printArr(int[] arr, int n) ????{ ????????for (int i = 0; i < n; i++) ????????????Console.Write(arr[i] + " "); ????} ????// Function to sort the array of 0s, 1s and 2s ????static void sortArr(int[] arr, int n) ????{ ????????int i, cnt0 = 0, cnt1 = 0, cnt2 = 0; ????????// Count the number of 0s, 1s and 2s in the array ????????for (i = 0; i < n; i++) { ????????????switch (arr[i]) { ????????????case 0: ????????????????cnt0++; ????????????????break; ????????????case 1: ????????????????cnt1++; ????????????????break; ????????????case 2: ????????????????cnt2++; ????????????????break; ????????????} ????????} ????????// Update the array ????????i = 0; ????????// Store all the 0s in the beginning ????????while (cnt0 > 0) { ????????????arr[i++] = 0; ????????????cnt0--; ????????} ????????// Then all the 1s ????????while (cnt1 > 0) { ????????????arr[i++] = 1; ????????????cnt1--; ????????} ????????// Finally all the 2s ????????while (cnt2 > 0) { ????????????arr[i++] = 2; ????????????cnt2--; ????????} ????????// Print the sorted array ????????printArr(arr, n); ????} ????// Driver code ????public static void Main() ????{ ????????int[] arr = { 0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1 }; ????????int n = arr.Length; ????????sortArr(arr, n); ????} } // This code is contributed by shubhamsingh10 ``` **輸出**: ``` 0 0 0 0 0 1 1 1 1 1 2 2 ``` * **復雜度分析**: * **時間復雜度**:`O(n)`。 僅需要兩次遍歷數組。 * **空間復雜度**:`O(1)`。 由于不需要額外的空間。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看