mardi 24 février 2015

What is the most efficient algorithm to find such number of pairs(i,j) such that the number of inversions becomes minimum

Assume I have an array of N numbers unsorted. To sort the array I use the following code:



for (int i = 1; i < n; i = i + 1)
{
int j = i;
while (j > 0 && a[j] < a[j - 1])
{
swap(a[j], a[j - 1]);
j = j - 1;
}
}


which is known as bubble sort as far as i know. To decrease the number of the inversions/call of swap function, i can pre-swap any two elements, say they have indexes i and j, then I perform the actual sort function. What is the most efficient algorithm to find such number of pairs(i,j) such that the number of inversions becomes minimum? Actually this a Codeforces problem and i also went through the tutorial and lots of accepted codes. But still I'm having difficulties in understanding the logic behind them. Since I'm a novice programmer could anyone please explain me the process starting from scratch? Thanks in advance :)


Aucun commentaire:

Enregistrer un commentaire