Why the second algorithm consume almost the same time as the first one?

Hi , everyone.

Could somebody explain why the second selection sort algorithm use much less time of copies but the time consumed is almost the same as the first one? I think it should be faster, is there something wrong with my way for testing?

Here is a testing result for sorting 10000 elements:

Algorithm 1:

Comparisons: 49995000 Copies: 146190

Time consumed: 1562

Algorithm 2

Comparisons: 49995000 Copies: 93071

Time consumed: 1578

Here is the complete Java program:

public class SelectionSort {

private static int selectionSortCompCounter = 0;

private static int selectionSortCopyCounter = 0;

// The first Selection Algorithm

static void selectionSort(Comparable[] a, int left, int right) {

// Sort the objects in a[left...right] into ascending order.

for (int l = left; l < right; l++) {

int p = l;

Comparable least = a[p];

for (int k = l + 1; k <= right; k++) {

int comp = a[k].compareTo(least);

// Increase comparions counter by 1.

selectionSortCompCounter++;

if (comp < 0) {

p = k;

least = a[p];

// Increase copies counter by 2;

selectionSortCopyCounter += 2;

}

}

if (p != l) {

a[p] = a[l];

a[l] = least;

// Increase copies counter by 2;

selectionSortCopyCounter += 2;

}

}

}

//The Second Selection Sort Algorithm

static void selectionSort2(Comparable[] a, int left, int right) {

for (int l = left; l < right; l++) {

int p = l;

for (int k = l + 1; k <= right; k++) {

int comp = a[k].compareTo(a[p]);

// Increase comparions counter by 1.

selectionSortCompCounter++;

if (comp < 0) {

p = k;

// Increase copies counter by 1;

selectionSortCopyCounter += 1;

}

}

if (p != l) {

Comparable temp = a[p];

a[p] = a[l];

a[l] = temp;

// Increase copies counter by 3;

selectionSortCopyCounter += 3;

}

}

}

static String random(int m) {

//Generate and return a random m-digit string.

char[] d = new char[m];

for (int i = 0; i < m; i++) {

d = (char) (10*Math.random() + '0');

}

return new String(d);

}

static String[] generateRandom(int n) {

String[] random = new String[n];

for (int i = 0; i < n; i++) {

random = random(3);

}

return random;

}

public static void main(String[] args) {

//For testing the first algorithm

String[] string = generateRandom(10000);

//For testing the second algorithm

String[] string2 = new String[10000];

// Makes string2[] contains exactly the same elements as string1[]

for(int i = 0; i < string.length; i++) {

string2 = string;

}

//Algorithm1 start time

long time1 = System.currentTimeMillis();

selectionSort(string, 0, 9999);

//Algorithm2 end time

long time2 = System.currentTimeMillis();

//Print the first algorithm result

System.out.println(selectionSortCompCounter + " " + selectionSortCopyCounter);

System.out.println("Time consumed: " + (time2 - time1));

//Let counters be 0

selectionSortCompCounter = 0;

selectionSortCopyCounter = 0;

//Algorithm2 start time

long time3 = System.currentTimeMillis();

selectionSort2(string2, 0, 9999);

//Algorithm2 end time

long time4 = System.currentTimeMillis();

//Print the second algorithm result

System.out.println(selectionSortCompCounter + " " + selectionSortCopyCounter);

System.out.println("Time consumed: " + (time4 - time3));

}

}

thx a lot ,guys :)

[3819 byte] By [K_Ghosta] at [2007-10-2 9:50:13]
# 1

Case 1:

49995000 comparisons

+146190 copies

-

50141190 total operations

Case 2:

49995000 comparisons

+93071 copies

50088071 total operations

total operations in case 1 is 50.1 million to 3 decimals

total operations in case 2 is 50.1 million to 3 decimals

And you want to know why the time to complete operation 2 matches the time to complete operation 1 out to two decimals?

marlin314a at 2007-7-16 23:55:25 > top of Java-index,Other Topics,Algorithms...
# 2
> 50141190 total operationsHow did you measure that? Interested to be able to do the same.Gi,
gilroittoa at 2007-7-16 23:55:25 > top of Java-index,Other Topics,Algorithms...
# 3

have you tried running the test backwark (run algorithm2 then run algorithm 1) and run it severals time.

the number of comparison are the same..the only differences is the number of copies, but they are relatively small..so the time differences should not be noticiable (because the operating system could kick in...also the JVM could be doing some side track stuff like quickly garbage collect..etc)

try increasing the size to 100,000 .. 500,000, etc..and see if there is a different.

for example..you wont really notice a differences if the the total number of element you're going to test is 10.

tnguyen1973a at 2007-7-16 23:55:25 > top of Java-index,Other Topics,Algorithms...
# 4

This is the answer explained by a professor in my department:

"The reason for your strange results is that you are miscounting copies in both versions of the algorithm. In Algorithm 2, for example, your swap should be counted as 2 copies, not 3.

Real time measurements on a computer can be unreliable: they can be influenced by the existence of other processes running concurrently with the process that you are trying to measure. Software engineers get over this by running the process repeatedly with the same data, and taking the average time."

I still have some questions now, he said I miscounting copies in both algorithm, and in algorithm2 the copies of swap should be 2 rather than 3, I don't know why the following code counts 2 times but not 3?

{

int a = b;

b = c;

c = a

}

What's more, what is the step in which I miscounted the copies in Algorithm1?

Thanks a lot , guys, waiting for your ideas :P

K_Ghosta at 2007-7-16 23:55:25 > top of Java-index,Other Topics,Algorithms...
# 5
Are you sure you understood what you professor told you, and that he understood your quesiton? It seems to me that counting the swap as 2 instead of 3 would make your results even more strange (based on your expectations) not less.
dubwaia at 2007-7-16 23:55:25 > top of Java-index,Other Topics,Algorithms...
# 6

Your Solution !!!

public class SelectionSort {

private static int selectionSortCompCounter = 0;

private static int selectionSortCopyCounter = 0;

static void selectionSort(Comparable[] a, int left, int right) {

int iIncrement = 0;

int iAssignment = 0;

int iComparison = 0;

int iRetrieve = 0;

for (int l = left; l < right; l++) {

iIncrement++;

iComparison++;

int p = l;

iAssignment++;

Comparable least = a[p];

iAssignment++;

iRetrieve++;

for (int k = l + 1; k <= right; k++) {

iIncrement++;

iComparison++;

int comp = a[k].compareTo(least);

iAssignment++;

iComparison++;

iRetrieve++;

// Increase comparions counter by 1.

selectionSortCompCounter++;

iIncrement++;

if (comp < 0) {

iComparison++;

p = k;

iAssignment++;

least = a[p];

iAssignment++;

iRetrieve++;

// Increase copies counter by 2;

selectionSortCopyCounter += 2;

iAssignment++;

iIncrement++;

}

}

if (p != l) {

iComparison++;

a[p] = a[l];

iAssignment++;

iRetrieve++;

iRetrieve++;

a[l] = least;

iAssignment++;

iRetrieve++;

// Increase copies counter by 2;

selectionSortCopyCounter += 2;

iAssignment++;

iIncrement++;

}

}

System.out.println("Comparison : " + iComparison);

System.out.println("Assigment : " + iAssignment);

System.out.println("Increment : " + iIncrement);

System.out.println("Retrieval : " + iRetrieve);

}

//The Second Selection Sort Algorithm

static void selectionSort2(Comparable[] a, int left, int right) {

int iIncrement = 0;

int iAssignment = 0;

int iComparison = 0;

int iRetrieve = 0;

for (int l = left; l < right; l++) {

iIncrement++;

iComparison++;

int p = l;

iAssignment++;

for (int k = l + 1; k <= right; k++) {

iIncrement++;

iComparison++;

int comp = a[k].compareTo(a[p]);

iAssignment++;

iComparison++;

iRetrieve++;

iRetrieve++;

// Increase comparions counter by 1.

selectionSortCompCounter++;

iIncrement++;

if (comp < 0) {

iComparison++;

p = k;

iAssignment++;

selectionSortCopyCounter += 1;

iIncrement++;

iAssignment++;

}

}

if (p != l) {

iComparison++;

Comparable temp = a[p];

iAssignment++;

iRetrieve++;

a[p] = a[l];

iAssignment++;

iRetrieve++;

iRetrieve++;

a[l] = temp;

iAssignment++;

iRetrieve++;

selectionSortCopyCounter += 3;

iIncrement++;

iAssignment++;

}

}

System.out.println("Comparison : " + iComparison);

System.out.println("Assigment : " + iAssignment);

System.out.println("Increment : " + iIncrement);

System.out.println("Retrieval : " + iRetrieve);

}

static String random(int m) {

char[] d = new char[m];

for (int i = 0; i < m; i++) {

d = (char) (10 * Math.random() + '0');

}

return new String(d);

}

static String[] generateRandom(int n) {

String[] random = new String[n];

for (int i = 0; i < n; i++) {

random = "" + random(i);

}

return random;

}

public static void main(String[] args) {

final int RANGE = 10000;

String[] string = generateRandom(RANGE);

String[] string2 = new String[RANGE];

System.arraycopy(string, 0, string2, 0, string.length);

selectionSortCompCounter = 0;

selectionSortCopyCounter = 0;

long time1 = System.currentTimeMillis();

selectionSort(string, 0, RANGE - 1);

long time2 = System.currentTimeMillis();

System.out.println(selectionSortCompCounter + " " + selectionSortCopyCounter);

System.out.println("Time consumed: " + (time2 - time1));

selectionSortCompCounter = 0;

selectionSortCopyCounter = 0;

long time3 = System.currentTimeMillis();

selectionSort2(string2, 0, RANGE - 1);

long time4 = System.currentTimeMillis();

System.out.println(selectionSortCompCounter + " " + selectionSortCopyCounter);

System.out.println("Time consumed: " + (time4 - time3));

}

}

Algorithm 1::

Comparison : 100088296

Assigment : 50279889

Increment : 100088296

Retrieval : 50113276

49995000 176594

Time consumed: 7406

Algorithm 2::

Comparison : 100088296

Assigment : 50201573

Increment : 100088296

Retrieval : 100029960

49995000 108277

Time consumed: 7156

Please check the retrieval and comparison ....

Basic Assembly Instruction ...

Regards...

myraid77a at 2007-7-16 23:55:25 > top of Java-index,Other Topics,Algorithms...
# 7
thx, I think that's the acurate solution, I neglected some assignment and retrival.
K_Ghosta at 2007-7-16 23:55:25 > top of Java-index,Other Topics,Algorithms...