Skip to main content
added 4 characters in body
Source Link
coderodde
  • 32.3k
  • 15
  • 79
  • 205

Your implementation will run in quadratic time in all cases. You can optimize here a bit:

public static voidint BubbleSort(int[] arr) {
    int loopCount = 0;

    for (int i = 1; i < arr.Length; ++i) {
        bool swapped = false;

        for (int j = 0; j < arr.Length - i; ++j) {
            loopCount++;

            if (arr[j] > arr[j + 1]) {
                swapped = true;
                int tmptemp = arr [j];
                arr [j] = arr[j + 1];
                arr [j + 1] = tmp;temp;
            }
        }

        if (!swapped) {
            return;break;
        }
    } 

    return loopCount;
}

The above alternative will run faster whenever the input array is "presorted," i.e., exhibits a lot of order.

Also, I would not waste CPU time by counting the number of inner loop iterations: after all, who cares?

Declaring the sort as static will allow the user to use your sort without actually instantiating the class it belongs to.

Last but not least: you can declare temptemp in the body of the inner loop, since it affects nothing.

Your implementation will run in quadratic time in all cases. You can optimize here a bit:

public static void BubbleSort(int[] arr) {
    for (int i = 1; i < arr.Length; ++i) {
        bool swapped = false;

        for (int j = 0; j < arr.Length - i; ++j) {
            if (arr[j] > arr[j + 1]) {
                swapped = true;
                int tmp = arr [j];
                arr [j] = arr[j + 1];
                arr [j + 1] = tmp;
            }
        }

        if (!swapped) {
            return;
        }
    }
}

The above alternative will run faster whenever the input array is "presorted," i.e., exhibits a lot of order.

Also, I would not waste CPU time by counting the number of inner loop iterations: after all, who cares?

Declaring the sort as static will allow the user to use your sort without actually instantiating the class it belongs to.

Last but not least: you can declare temp in the body of the inner loop, since it affects nothing.

Your implementation will run in quadratic time in all cases. You can optimize here a bit:

public static int BubbleSort(int[] arr) {
    int loopCount = 0;

    for (int i = 1; i < arr.Length; ++i) {
        bool swapped = false;

        for (int j = 0; j < arr.Length - i; ++j) {
            loopCount++;

            if (arr[j] > arr[j + 1]) {
                swapped = true;
                int temp = arr [j];
                arr [j] = arr[j + 1];
                arr [j + 1] = temp;
            }
        }

        if (!swapped) {
            break;
        }
    } 

    return loopCount;
}

The above alternative will run faster whenever the input array is "presorted," i.e., exhibits a lot of order.

Declaring the sort as static will allow the user to use your sort without actually instantiating the class it belongs to.

Last but not least: you can declare temp in the body of the inner loop, since it affects nothing.

Source Link
coderodde
  • 32.3k
  • 15
  • 79
  • 205

Your implementation will run in quadratic time in all cases. You can optimize here a bit:

public static void BubbleSort(int[] arr) {
    for (int i = 1; i < arr.Length; ++i) {
        bool swapped = false;

        for (int j = 0; j < arr.Length - i; ++j) {
            if (arr[j] > arr[j + 1]) {
                swapped = true;
                int tmp = arr [j];
                arr [j] = arr[j + 1];
                arr [j + 1] = tmp;
            }
        }

        if (!swapped) {
            return;
        }
    }
}

The above alternative will run faster whenever the input array is "presorted," i.e., exhibits a lot of order.

Also, I would not waste CPU time by counting the number of inner loop iterations: after all, who cares?

Declaring the sort as static will allow the user to use your sort without actually instantiating the class it belongs to.

Last but not least: you can declare temp in the body of the inner loop, since it affects nothing.