Jump to content

"Costed" Ascending Sort Algorithm

- - - - -

  • Please log in to reply
No replies to this topic

#1
CoryG89

CoryG89

    Newbie

  • Members
  • Pip
  • 2 posts
OK, so I have a project going, where I take an input file, consisting of a number of lines with a number of integers (no more than 10) separated by a single space. Each of the integers in between 1-100 in value. These lines are unsorted.

I am to take each line and sort it in ascending order. But swapping two of the integers' positions incurs a cost which is equal to the sum of the two integers.

So for the input line: "3 2 1"

We could simply swap the 1 and the 3 for a single swap that incurs a cost of: (1+3) = 4.

Of course more integers on each line is going to require more swaps and a higher cost.
The number of swaps that are made to sort the sequence is not important. Only the total cost of the sort. I need to calculate the sequence of swaps that are necessary to sort the sequence incurring the minimum cost possible.

Here are some examples:

Sequence 1 (input): "3 2 1"
Sequence 2 (input): "8 1 2 4"
Sequence 3 (input): "1 8 9 7 6"

Sequence 1 (cost): 4
Sequence 2 (cost): 17
Sequence 3 (cost): 41


Ok, so thinking about the problem and I have thought about some things that might help me accomplish this.

(1). I know that I can take a sequence of numbers and calculate all the 2-subsets of it (which would represent all possible unique swaps). I am not sure how I would go about generating all of these sequences, or for a sequence of 10 integers if it would be unfeasibly expensive computationally. Anyone know of a good way to do this? To calculate the number of such 2-subsets of a particular sequence would be:

x = n!/[2(n-2)!] (Where n is the number of integers in the original sequence).


(2). I have looked at a set of general purpose algorithms that might would help me. Especially the Branch & Bound algorithm. It seems that if I could calculate all of the possible swaps, then in turn all of the possible sequence of swaps that would sort the original sequence of integers. If I could manage this, I could place all of the sequences of swaps into a search tree and use this Branch and Bound algorithm to eliminate all of the higher cost sequences of swaps.


I am not looking for straight code that will solve my problem. Just a point in the right direction. Or does anyone know if the train of thought I have going here will work, if not how can I change it to get on the right track. Also, would this be considered 'brute forcing' as I do not want to do this to solve this problem. Also with an original sequence of 10 integers would this take an infeasible amount of time to run?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users