Cyclic Sort pattern



John Rankin @ May 10, 2023

The Cyclic Sort pattern is used when dealing with an array containing numbers in a given range. It involves iterating through the array and placing each number at its correct index by swapping elements. Here's an example to help you understand the pattern better:

Problem: Given an unsorted array containing numbers from 1 to n with one number missing, find the missing number.

Example:

Input: [4, 2, 3, 1, 6] Output: 5

In this problem, the array should contain numbers from 1 to n, but the number 5 is missing. We can solve this problem using the Cyclic Sort pattern as follows:

def find_missing_number(nums):
    i = 0
    while i < len(nums):
        correct_index = nums[i] - 1  # Calculate the index where the current number should be
        if nums[i] != nums[correct_index]:  # If the number is not at its correct index, swap
            nums[i], nums[correct_index] = nums[correct_index], nums[i]
        else:
            i += 1

    # Find the missing number
    for i in range(len(nums)):
        if nums[i] != i + 1:
            return i + 1

    # If no missing number found, return n + 1
    return len(nums) + 1

Let's analyze the code:

  1. We start by initializing the i pointer to 0 and iterate through the array.
  2. For each number nums[i], we calculate the correct index where it should be placed (nums[i] - 1).
  3. If the number is not already at its correct index, we swap it with the number at its correct index.
  4. If the number at the current index is already in its correct position, we move to the next index.
  5. After the cyclic sort is complete, we iterate through the sorted array and find the missing number (the first number that is not at its correct index).
  6. If no missing number is found, we return len(nums) + 1 as the missing number (since all numbers from 1 to n are present).

Now, let's test the function using the example input:

 
nums = [4, 2, 3, 1, 6]
missing_number = find_missing_number(nums)
print(missing_number)  # Output: 5

The missing number is correctly identified as 5 using the Cyclic Sort pattern.

The Cyclic Sort pattern can be applied to solve various other problems, such as finding the smallest missing positive number, finding all missing numbers, or finding duplicate numbers. The underlying idea remains the same - placing each number at its correct index using swaps.



 

 

Most Recent Posts


Bitwise XOR-exclusive OR

Read This
May 10, 2023

0-1 Knapsack

Read This
May 10, 2023

K-Way Merge

Read This
May 10, 2023

Subsets

Read This
May 10, 2023

Backtracking

Read This
May 10, 2023

Greedy Algorithm

Read This
May 10, 2023

Trie

Read This
May 10, 2023

Depth-First Search-DFS

Read This
May 10, 2023

Breadth-First Search-BFS

Read This
May 10, 2023

Binary Search Tree-BST

Read This
May 10, 2023

Top K Elements

Read This
May 10, 2023

Binary Search Pattern

Read This
May 10, 2023

Cyclic Sort pattern

Read This
May 10, 2023

Merge Intervals

Read This
May 10, 2023

Day 7 - DynamoDB - and Working with 2 Services - Lambda

Read This
August 25, 2018

Day 6 - Lambda - Creating an API

Read This
August 23, 2018

AWS - Day 5 - S3 - Simple Storage Service

Read This
August 22, 2018

AWS - Day 4 - AWS CLI Useful Scripts

Read This
August 21, 2018

AWS - Day 3 - Create Container from another container

Read This
August 20, 2018

Day 2 - Docker Intro

Read This
August 17, 2018

AWS - Day1 - Tutorial - Launching my first Docker Container

Read This
August 16, 2018

AWS - Day 1 - Signing up and testing out their tutorials

Read This
August 16, 2018

Dynamic Programming - Edit Distance

Read This
December 19, 2016

Dynamic Programming - Fibonacci

Read This
December 19, 2016