English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

2007-02-24 13:33:23 · 4 answers · asked by Galbadian 2 in Computers & Internet Programming & Design

I have an array of 27 numbers. I want to find the two lowest values, add them, and then repeat that process for next two lowest values (including the result of adding the lowest 2 if it is part of the new lowest two objects) and so on.

2007-02-24 13:38:45 · update #1

Note: This procedure, while seemingly trivial, is actually part of the process for encoding bits. Ever heard of Huffman Coding? That's what this is for.

2007-02-24 15:05:31 · update #2

4 answers

The previous answer is mighty good!

I was just thinking you could sort the array from lowest to highest.
Then you would take the fron 2 values, add them, and INSERT the result into the array (insertion sort).

Each time you do this, the overall size of the array is reduced by 1.

2007-02-24 14:21:56 · answer #1 · answered by Alan 6 · 0 0

What exactly is the point of doing that? If you keep repeating it you end up with the sum of all the numbers in the array anyway, regardless of what order they are added in.

But for what it's worth, I like the second answer. There's no point writing a load of code from scratch when you can re-use commonly used code anyway, i.e insertion sort, and just writing a small bit yourself to deal with adding the too lowest numbers which is a very trivial task.

2007-02-24 23:01:37 · answer #2 · answered by kevosully 2 · 0 0

This can be done with looping and iteration.

Make 2 variables and a for loop,

create a sequential algorithms in the loop to replace the 2 variables whenever they encounter something that is lower than either of them, compare them to each other, then swap if necessary.

2007-02-25 00:31:14 · answer #3 · answered by D 4 · 0 0

I'd use a logical switch statement, nested in a while.

double swapDoub = 0.0;
double firstHighest = array[0];
double secondHighest = 0.0;

while (i < (array.length()-1))
switch (Double.compare(firstHighest,array[i+1])
{
{
case 0:
case 1:
// If first highest is greater than array element,
// compare to second
switch (Double.compare(secondHighest,array[i+1]){
case 0:
case 1:
// If second highest is greater than array element, increment i
i++
break;
case -1:
// If second highest is less than array element,
// assign array element to second highest.
secondHighest = array[i+1]
i++
break;
break;
case -1:
// If first highest is less than array element, assign to first highest
swapDoub = firstHighest // Swap values.
firstHighest = array [i+1]
switch (Double.compare(swapDoub,secondHighest){
case 0:
case 1:
// If swapDoub > secondHighest, insert value
secondHighest = swapDoub
i++
break;
case -1:
// If swapDoub less than secondHighest, increment
i++
break;
break;
}
}

Once it kicks free of the loop, sum the two Highest, reinsert into a new array of array.length - 1 elements, repeat.

2007-02-24 21:52:43 · answer #4 · answered by Anonymous · 0 0

fedest.com, questions and answers