📅  最后修改于: 2020-12-10 06:37:06             🧑  作者: Mango
荷兰国旗(DNF)-这是Edsger Dijkstra提出的编程问题。荷兰的国旗由三种颜色组成:白色,红色和蓝色。任务是以相同颜色的球放置在一起的方式随机排列白色,红色和蓝色的球。对于DNF(荷兰国旗),我们在线性时间内对数组0、1和2进行了排序,而不会占用任何额外的空间。我们必须记住,该算法只能在具有三个唯一元素的数组上实现。
#include
using namespace std;
// Function to sort the input array where the array is assumed to have values in {0, 1, 2}
// We have to take 3 distint or unique elements
void JTP(int arr[], int arr_size)
{
int low = 0;
int high = arr_size - 1;
int mid = 0;
// We have keep iterating till all the elements are sorted
while (mid <= high)
{
switch (arr[mid])
{
// Here mid is 0.
case 0:
swap(arr[low++], arr[mid++]);
break;
// Here mid is 1.
case 1:
mid++;
break;
// Here mid is 2.
case 2:
swap(arr[mid], arr[high--]);
break;
}
}
}
// Now, we write the function to print array arr[]
void printArray(int arr[], int arr_size)
{
// To iterate and print every element, we follow these steps
for (int i = 0; i < arr_size; i++)
cout << arr[i] << " ";
}
//Main Code
int main()
{
int arr[] = {0,1,0,1,2,0,1,2};
int n = sizeof(arr)/sizeof(arr[0]);
cout << "Array before executing the algorithm: ";
printArray(arr, n);
JTP(arr, n);
cout << "\nArray after executing the DNFS algorithm: ";
printArray(arr, n);
return 0;
}
输出-
执行算法之前的数组:0 1 0 1 2 0 1 2
执行DNFS算法后的数组:0 0 0 1 1 1 2 2
import java.io.*;
class DNF {
static void JTP(int arr[], int arr_size)
{
int low = 0;
int high = arr_size - 1;
int mid = 0, temp=0; // We use temporary variable for swapping
while (mid <= high)
{
switch (arr[mid])
{
case 0: // Here mid pointer points is at 0.
{
temp = arr[low];
arr[low] = arr[mid];
arr[mid] = temp;
low++;
mid++;
break;
}
case 1: // Here mid pointer points is at 1.
mid++;
break;
case 2: // Here mid pointer points is at 2.
{
temp = arr[mid];
arr[mid] = arr[high];
arr[high] = temp;
high--;
break;
}
}
}
}
// Now we have to call 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("");
}
//Now we use driver function to check for above functions
public static void main (String[] arguments)
{
int arr[] = {0, 1, 0, 1, 2, 0, 1, 2};
int arr_size = arr.length;
System.out.println("Array before executing the DNFS algorithm : ");
printArray(arr, arr_size);
JTP(arr, arr_size);
System.out.println("\nArray after executing the DNFS algorithm : ");
printArray(arr, arr_size);
}
}
输出-
执行DNFS算法之前的数组:0 1 0 1 2 0 1 2
执行DNFS算法后的数组:0 0 0 1 1 1 2 2 2
def JTP( arr, arr_size):
low = 0
high = arr_size - 1
mid = 0
while mid <= high:
if arr[mid] == 0:
arr[low],arr[mid] = arr[mid],arr[low]
low = low + 1
mid = mid + 1
elif arr[mid] == 1:
mid = mid + 1
else:
arr[mid],arr[high] = arr[high],arr[mid]
high = high - 1
return arr
# Function to print array
def printArray(arr):
for k in arr:
print k,
print
# Driver Program
arr = [0, 1, 0, 1, 1, 2, 0, 1, 2]
arr_size = len(arr)
print " Array before executing the algorithm: ",
printArray(arr)
arr = JTP(arr, arr_size)
print "Array after executing the DNFS algorithm: ",
printArray(arr)
输出-
执行算法之前的数组:0 1 0 1 1 2 0 1 2
执行DNFS算法后的数组:0 0 0 1 1 1 1 2 2 2