Skip to content

Commit 434e9bc

Browse files
committed
Update sortColors doc
1 parent d22b3b9 commit 434e9bc

File tree

6 files changed

+76
-36
lines changed

6 files changed

+76
-36
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ List of Programs related to data structures and algorithms
5151
6. Minimum size subarray sum: [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/6.minimumSizeSubarraySum/minSizeSubarraySum.js)
5252
[Playground](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/6.minimumSizeSubarraySum/minSizeSubarraySum.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/6.minimumSizeSubarraySum/minSizeSubarraySum.md)
5353

54-
7. Sort Colors: [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/sortColors/sortColors.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/sortColors/sortColors.md)
55-
54+
7. Sort Colors: [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/7.sortColors/sortColors.js)
55+
[Playground](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/7.sortColors/sortColors.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/7.sortColors/sortColors.md)
5656

5757
8. Maximum product subarray: [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/maxProductSubArray.js)
5858

src/java1/algorithms/array/sortColors/SortColors.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ private static void swap(int[] nums, int i, int j) {
3131
}
3232

3333
public static void main(String[] args) {
34-
int[] nums1 = {2,0,2,1,1,0};
35-
int[] nums2 = {2,0,1};
34+
int[] nums1 = {2,1,0,1,2,0};
35+
int[] nums2 = {2,1,0};
3636
System.out.println(Arrays.toString(sortColors(nums1)));
3737
System.out.println(Arrays.toString(sortColors(nums2)));
3838

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,40 @@
1-
**Algorithmic Steps**
2-
This problem is solved with the help of two pointers technique. It is also know as "Dutch National Flag problem". The algorithmic approach can be summarized as follows:
1+
**Description:**
2+
Given an array `nums` with `n` objects colored red, white, where each object is color coded as integer: blue, 0 for red, 1 for white, and 2 for blue. We need to sort them in-place so that objects of the same color are grouped together, with the colors in the order red, white, and blue.
33

4+
This problem needs to be solved without using the library's sort function.
45

5-
1. Initialize left and right pointers to first(i.e, 0) and end index of the array, to keep track of the current window boundaries.
6+
### Examples
67

7-
3. Initialize current index pointer(i.e, i) to 0, to keep track of the current character while iterating the array.
8+
Example 1:
89

9-
4. Iterate over the input array using index pointer until the end of the array.
10+
Input: nums = [2,1,0,1,2,0]
11+
Output: [0,0,1,1,2,2]
1012

11-
5. If the current character is equals to 0, swap the character values at left pointer and index pointer. Also, increment the left pointer and index pointer.
13+
Example 2:
1214

13-
6. If the current character is equals to 2, swap the character values at right pointer and index pointer. Also, decrement the right pointer. If the chracter is neither 0 or 2, then just increment the index pointer.
15+
Input: nums = [2,1,0]
16+
Output: [0,1,2]
1417

15-
8. Repeat steps 5–6 until the index pointer reaches the end of the array.
18+
**Algorithmic Steps:**
1619

17-
9. Return the updated in-place array where chracters are sorted.
20+
This problem is solved with the help of **two pointers** technique. It is also know as **"Dutch National Flag problem"**. The algorithmic approach can be summarized as follows:
21+
22+
23+
1. Initialize both left and right pointers to first index(i.e, `0`) and end index(i.e, `nums.length-1`) of the array respectively, to keep track of the current window boundaries.
24+
25+
2. Initialize current index pointer(i.e, `i`) to 0, to keep track of the current character while iterating the array.
26+
27+
3. Iterate over the input array using index pointer until the end of the array.
28+
29+
4. If the current character is equals to 0, swap the character values at left pointer and index pointer. Also, increment the left pointer and index pointer.
30+
31+
5. If the current character is equals to 2, swap the character values at right pointer and index pointer. Also, decrement the right pointer.
32+
33+
6. If the chracter is neither `0` or `2`, then just increment the index pointer.
34+
35+
7. Repeat steps 4–6 until the index pointer reaches the end of the array.
36+
37+
8. Return the updated in-place array where chracters are sorted.
1838

1939
**Time and Space complexity:**
20-
This algorithm has a time complexity of O(n) because we are traversing the array only once. Also, it takes requires space complexity of O(1) because we are updating the array in-place without using an additional array.
40+
This algorithm takes a time complexity of `O(n)` because we are traversing the array only once. Also, it requires space complexity of `O(1)` because we are updating the array in-place without using an additional data structure.

src/javascript/algorithms/array/sortColors/sortColors.js renamed to src/javascript/algorithms/array/7.sortColors/sortColors.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ function swap(nums, one, two) {
2626
nums[two] = temp;
2727
}
2828

29-
let nums1 = [2,0,2,1,1,0];
29+
let nums1 = [2,1,0,1,2,0];
3030
console.log(sortColors(nums1));
3131

32-
let nums2 = [2,0,1];
32+
let nums2 = [2,1,0];
3333
console.log(sortColors(nums2));
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
**Description:**
2+
Given an array `nums` with `n` objects colored red, white, where each object is color coded as integer: blue, 0 for red, 1 for white, and 2 for blue. We need to sort them in-place so that objects of the same color are grouped together, with the colors in the order red, white, and blue.
3+
4+
This problem needs to be solved without using the library's sort function.
5+
6+
### Examples
7+
8+
Example 1:
9+
10+
Input: nums = [2,1,0,1,2,0]
11+
Output: [0,0,1,1,2,2]
12+
13+
Example 2:
14+
15+
Input: nums = [2,1,0]
16+
Output: [0,1,2]
17+
18+
**Algorithmic Steps:**
19+
20+
This problem is solved with the help of **two pointers** technique. It is also know as **"Dutch National Flag problem"**. The algorithmic approach can be summarized as follows:
21+
22+
23+
1. Initialize both left and right pointers to first index(i.e, `0`) and end index(i.e, `nums.length-1`) of the array respectively, to keep track of the current window boundaries.
24+
25+
2. Initialize current index pointer(i.e, `i`) to 0, to keep track of the current character while iterating the array.
26+
27+
3. Iterate over the input array using index pointer until the end of the array.
28+
29+
4. If the current character is equals to 0, swap the character values at left pointer and index pointer. Also, increment the left pointer and index pointer.
30+
31+
5. If the current character is equals to 2, swap the character values at right pointer and index pointer. Also, decrement the right pointer.
32+
33+
6. If the chracter is neither `0` or `2`, then just increment the index pointer.
34+
35+
7. Repeat steps 4–6 until the index pointer reaches the end of the array.
36+
37+
8. Return the updated in-place array where chracters are sorted.
38+
39+
**Time and Space complexity:**
40+
This algorithm takes a time complexity of `O(n)` because we are traversing the array only once. Also, it requires space complexity of `O(1)` because we are updating the array in-place without using an additional data structure.

src/javascript/algorithms/array/sortColors/sortColors.md

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)