Minimum Swaps Required to Sort an Array

An efficient algorithm to find the minimum number of swaps required to sort the array in ascending order.
Problem:
We have an unordered array consisting of consecutive distinct integers ∈ [1,2,3,...n], where n is the size of the array.
We are allowed to swap any two elements.
We need to find the minimum number of swaps required to sort the array in ascending order.
Algorithm Concept:
1. Look at each index and compare the index position with its element value if its same then move to the next index position.
2. If index position is not the same as element value then treat element value as index value for finding the next element.
3. If we come back to the visited element then there exist a cycle, then count the size of that cycle, the number of swaps for particular cycle would be size-1, do this for all the cycles and add them together.
Outline (check the comment section for a clickable version):
00:00 : Introduction
00:06 : Problem
00:52 : Wrong Approach (Brute Force) Method with Animation Example
01:31 : Algorithm Concept
02:23 : Efficient Solution with Animation Example
05:44 : Code Implementation of Algorithm
Code Available At:
sites.google.com/view/minswaps

Пікірлер: 32

  • @DSAlgo
    @DSAlgo3 жыл бұрын

    Minimum Swaps Required to Sort an Array [Non-Consecutive Distinct Integers] : kzread.info/dash/bejne/aoypw6ywgZW2Zc4.html

  • @PradeepKumar-ue2ct

    @PradeepKumar-ue2ct

    3 жыл бұрын

    Can you please mention time complexity of this solution?

  • @DSAlgo

    @DSAlgo

    3 жыл бұрын

    Time complexity to find the minimum swaps is mentioned below: i. Using Efficient algorithm is O(n log(n)). ii. Using Selection sort algorithm is O(n^2).

  • @anandsehgal8845
    @anandsehgal88454 жыл бұрын

    Great!! You have Explained well and implemented optimal solution

  • @arunavdey8579
    @arunavdey85793 жыл бұрын

    Helpful and clear. Thanks !

  • @vikashpanchal9609
    @vikashpanchal96094 жыл бұрын

    Really very effective approach to reduce swap counts.the way of explaining is very good and understandable. Thankyou so much.

  • @deepaligarg2978
    @deepaligarg29782 жыл бұрын

    Thanks for the explanation.

  • @purut5105
    @purut51054 жыл бұрын

    Nice Explanation!

  • @AnkitSingh-gi5zw
    @AnkitSingh-gi5zw4 жыл бұрын

    Awesome. Very nicely and clearly explained !!

  • @kratikamaheshwari421
    @kratikamaheshwari4214 жыл бұрын

    Thanxx.. I really enjoyed the way you explained the concept.

  • @ruchirsrivastava6808
    @ruchirsrivastava68084 жыл бұрын

    Very good explanation. Looking forward to more videos.

  • @akshatgupta7863
    @akshatgupta78634 жыл бұрын

    gr8 video!! make more videos for common problems

  • @kirtirai4502
    @kirtirai45024 жыл бұрын

    Your handwriting is so good

  • @devidrsr
    @devidrsr4 жыл бұрын

    Awesome, keep it up 👍😇

  • @abhinavghosh725
    @abhinavghosh7254 жыл бұрын

    this will only work for non duplicate values. nevertheless good work

  • @SudhanshuKumar-lp1nr

    @SudhanshuKumar-lp1nr

    3 жыл бұрын

    yes it's always distinct elements

  • @garimapandey1006
    @garimapandey10064 жыл бұрын

    Nice Content with gud explanation 👍

  • @rengaisok
    @rengaisok4 жыл бұрын

    I understand the logic can someone explain why this works?

  • @MaXxamillion00

    @MaXxamillion00

    4 жыл бұрын

    This works this way because this case is somewhat unique: all elements in the array are unique, and once sorted, all element values match their corresponding indices.... as in 1 is in position 1, 2 will be in position 2, 4 in 4, etc. That is why you can find one element out of place(because index!=value) and immediately jump to the place it SHOULD BE (index==value), and do the same thing with the out-of-place element found there. Eventually you will find the element that belongs to the initial index you were checking, the first out-of-place element, and that whole series of elements becomes a "cycle", you can perform an exact finite series of swaps to get all of those elements into their corresponding indices... like the video describes haha but long story short it works this way because in this array, once sorted, index==element for all indices.

  • @sumanth5087
    @sumanth50873 жыл бұрын

    Anyone have python code

  • @amitojsingh9373
    @amitojsingh93734 жыл бұрын

    pretty simple explanation

  • @shivammishrasvnit5456
    @shivammishrasvnit54563 жыл бұрын

    nice

  • @aisakyunhotahai8130
    @aisakyunhotahai81304 жыл бұрын

    Your handwriting is awesome😁😂

  • @Anubis10110
    @Anubis101104 жыл бұрын

    The explanation is AWESOME but the code is broken. could you please check it

  • @DSAlgo

    @DSAlgo

    4 жыл бұрын

    Thanks Mohamed, The code is working fine and code link is available in the description.

  • @Anubis10110

    @Anubis10110

    4 жыл бұрын

    @@DSAlgo Thanks I will check it..It's appearing now.

  • @AnkitSharma-hk8yq
    @AnkitSharma-hk8yq4 жыл бұрын

    We can use selection sort which always takes least no. of swappings to sort an array. In the given array selection sort will take 1 swapping only.

  • @DSAlgo

    @DSAlgo

    3 жыл бұрын

    Thanks for suggestion, The selection sort algorithm is not an efficient solution for this problem. Please see the explanation below: The selection sort algorithm is a brute force approach i.e. more computational effort is required. Therefore, It is not an optimal solution for finding the minimum swaps required to sort an unordered array consisting of consecutive distinct integers ∈ [1,2,3,...n]. e.g. [4,3,2,1] [1,5,4,3,2] We can use the selection sort algorithm for unordered array consisting of distinct integers ∈ Z i.e. {..., -2, -1, 0, 1, 2, ...}. e.g. [2,4,1,5] [4, 10, 2, 1, 0] kzread.info/dash/bejne/aoypw6ywgZW2Zc4.html - Selection sort algorithm approach.