Description: (minimum swaps)
Given a sequence, we have to find the minimum no of SWAPs required to sort the sequence.
Analysis:
Formula: No. of elements out of place-"cycles" in the sequence
A cycle is a set of elements, each of which is in the place of another. so in example SEQUENCES {2, 1, 4, 3}, there are two cycles: {1, 2} and {3, 4 }. 1 is in the place where 2 needs to go, and 2 is in the place where 1 needs to go 1, so they are a cycle; likewise with 3 and 4.
The sequences {3, 2, 1, 5, 6, 8, 4, 7} also has two cycles: 3 is in the place of 1 which is in the place of 3, so {1, 3} is a cycle; 2 is in its proper place; and 4 is in the place of 7 which is in the place of 8 in place of 6 in place of 5 in place of 4, so {4, 7, 8, 6, 5} is a cycle. there are seven elements out of place in two cycles, so five SWAps are needed.
Implementation:
E.g. {2, 3, 1, 5, 6, 4}
231564-> 6 Mismatch
Two cycles-> 123 and 456
Swap then, then 5 5 5-> 4 swaps to sort
Probably the easiest algorithm wocould be to use a bitarray. initialize it to 0, then start at the first 0. swap the number there to the right place and put a 1 there. continue until the current place holds the right number. then move on to the next 0
Example:
231564
000000
-> Swap 2 and 3
321564
010000
-> Swap 3, 1
123564
111000
-> Continue at next 0; Swap 5, 6
123654
111010
-> Swap 6, 4
123456
111111
-> Bitarray is all 1's, so we're all.
Code:
# Include <iostream>
# Include <algorithm>
Using namespace STD;
Template <typename T>
Int getminimumswapsforsorted (T seq [], int N)
{
Bool * right_place_flag = new bool [N];
T * sorted_seq = new T [N];
Int p, q;
////
Copy (SEQ, seq + N, sorted_seq );
Sort (sorted_seq, sorted_seq + n); // you can use a more efficient sorting algorithm.
////
For (INT I = 0; I <n; I ++)
{
If (SEQ [I]! = Sorted_seq [I])
Right_place_flag [I] = false;
Else
Right_place_flag [I] = true;
}
////
P = 0;
Int minimumswap = 0;
While (1)
{
While (right_place_flag [p])
P ++;
Q = p + 1;
/// This method can be used to obtain the minimum SWAps only for non-repeating sequences.
While (q <n)
{
If (! Right_place_flag [Q] & sorted_seq [Q] = seq [p])
Break;
Q ++;
}
If (q> = n | P> = N)
Break;
Right_place_flag [Q] = true;
If (SEQ [Q] = sorted_seq [p])
Right_place_flag [p] = true;
Swap (SEQ [p], seq [Q]);
Minimumswap ++;
}
Delete [] sorted_seq;
Delete [] right_place_flag;
Return minimumswap;
}
Int _ tmain (INT argc, _ tchar * argv [])
{
Int seq [] = {3, 2, 1, 5, 6, 8, 4, 7}; // {2, 3, 1, 5, 6, 4}; // {2, 3, 2, 4, 7,6, 3,5 };
Int n = sizeof (SEQ)/sizeof (INT );
Cout <"minimum swaps:" <getminimumswapsforsorted (SEQ, n) <Endl;
System ("pause ");
Return 0;
}
Description: (delete all repeated elements in the sequence)
Given an unsorted N element array [0 .... n-1] not containing any numbers outside the range 1 to n. we need to remove all duplicates from this array in constant space and O (n) time.
Analysis:
Array [0... n-1] value: [1, N]. You can use a conversion method to mark the elements in the sequence. Then, output these elements to remove all repeated elements in the sequence, the time complexity is O (n)
Code:
# Include <iostream>
# Include <math. h>
Using namespace STD;
Void removeallduplicates (INT seq [], int N)
{
// If Element E exists in the array, make index (E-1) Negative
For (INT I = 0; I <n; I ++)
SEQ [ABS (SEQ [I])-1] =-ABS (SEQ [ABS (SEQ [I])-1]);
// If index (E-1) is negative, print Element E
Cout <Endl;
For (INT I = 0; I <n; I ++)
If (SEQ [I] <0)
Cout <I + 1 <"/t ";
Cout <Endl;
}
Int _ tmain (INT argc, _ tchar * argv [])
{
Int seq [] = {5, 3, 4, 2, 3, 2, 5 };
Removeallduplicates (SEQ, 8 );
System ("pause ");
Return 0;
}