dutch-national-flag-problem

Dutch National Flag Problem Running in O(n)

亡梦爱人 提交于 2021-02-11 14:37:55
问题 I am a 10th-grade student in CompSci 1. In our textbook, Practical Programming 3rd Edition, An Introduction to Computer Science Using Python 3.6, it mentions the Dutch National Flag Problem. The following is how it states the exercise, word for word: Edsgar Dijkstra is known for his work on programming languages. He came up with a neat problem that he called the Dutch National Flag problem: given a list of strings, each of which is either "red", "green", or "blue" (each is represented several

Sorting in scheme following a pattern

混江龙づ霸主 提交于 2019-12-24 14:26:57
问题 A little help, guys. How do you sort a list according to a certain pattern An example would be sorting a list of R,W,B where R comes first then W then B. Something like (sortf '(W R W B R W B B)) to (R R W W W B B B) Any answer is greatly appreciated. 回答1: This is a functional version of the Dutch national flag problem. Here are my two cents - using the sort procedure with O(n log n) complexity: (define sortf (let ((map '#hash((R . 0) (W . 1) (B . 2)))) (lambda (lst) (sort lst (lambda (x y) (

Dutch National Flag - Not working for larger array

徘徊边缘 提交于 2019-12-23 16:59:50
问题 My below Dutch National Flag solution doesn't seem to work for the given input array containing only 3 elements - 0, 1 and 2. If I reduced the size of array, it works. I'm not able to identify the error. Am I missing something ? class DNF{ public static void sort(int [] arr, int size, int low, int high) { if(size == 0) return; int lower = 0; int upper = size - 1; while(lower < size && arr[lower] == low) lower++; while(upper >=0 && arr[upper] == high) upper--; int temp = 0; int pivot; for

Dutch national flag algorithm with four colors

半腔热情 提交于 2019-12-23 06:40:22
问题 I went through a solution for two and three colors but I am unable to get it for four colors. Please help. Will it be rrbb????yyyggg ? How will we swap the green flag? I tried the solution below but it is not working with swapping the last yellow with green. public class count123 { // Java program to sort an array of 0, 1 and 2,3 static void sort0123(int a[], int arr_size) { int lo = 0; int hi = arr_size - 1; int mid = 0,temp=0; int h2=arr_size - 1; while (mid <= hi) { switch (a[mid]) { case