Gist / June 23, 2023 / 1 min read / By Mahi Garg

Sort Int Array of 0, 1 and 2 : Swift (Dutch National Flag )

The Dutch National Flag algorithm is a sorting algorithm that partitions an array into three sections: elements less than a given pivot, elements equal to the pivot, and elements greater than the pivot. In Swift, we can implement this algorithm by maintaining three pointers and swapping elements accordingly. The time complexity of the Dutch National Flag algorithm is O(n), where n is the number of elements in the array. It is an efficient algorithm for sorting arrays with a limited range of values, like sorting colors or other categorical data.