简体   繁体   中英

Space complexity of bubble sort algorithm

i am trying to do a study on Space complexity of bubble sort algorithm what i know that the Space complexity of bubble sort algorithm is O(1) given the below bubble sort algorithm how can i change the bubble sort aalgorthim code to make the space or memory complexity to O(n) or O(n square) , etc i need to understand where the space complexity playes a role ...thanks

 public void bubbleSort(int[] arr) {
    boolean swapped = true;
    int j = 0;
    int tmp;

    while (swapped) {

        swapped = false;
        j++;

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

The space complexity is a measure of how much extra memory your algorithm requires.

If you were to allocate an extra array of size n (when n is the variable size of the input array), the space complexity would be O(n) .

If you want to increase space complexity you just need to waste memory eg add some code to use more memory.

Its decreasing space complexity which is hard.

I think it worths an answer, because it has some input on big O notation:

Your algorithm already is O(n) and O(n^2) space

This is because O(1) is a subset of O(n) and both are subsets of O(n^2)

Why is it so?
Note that O(f(n)) is a set of functions with "asymptotic upper bound of f(n)" (intuitive definition, not formal).

Thus, for each g(n)<h(n)<f(n) , if h(n) is an asymptotic upper bound of g(n) , then f(n) is also asymptotic upper bound of it.

Thus, if g(n) is in O(h(n)) - it is also in O(f(n))
And in your case, if the complexity function T(n) is in O(1) , it is also in O(n)

您的算法已经是 O(n) 空间,因为您至少需要 n 个内存单元

Here space complexity of your algorithm is O(n) and Auxiliary Space complexity it O(1).

In general we do compare Auxiliary complexity. why ?

Merge-Sort takes O(n) auxiliary space and Insertion sort requires O(1) auxiliary space Space complexity of these two sorting algorithms is O(n) though.

Bubble sort(A,n)
  for(i=n;i>=1;i--)
     for(j=1;j<=i-1;j++)
       if(a[j]>a[j+1])
       {
         t <- a[j]
         a[j] <- a[j+1]
         a[j+1] <- t
       }

Here we show input variable and temporary variable for space complexity then i and j are input variable of which space complexity always be constant and temporary variable t always be show 1 so space complexity of bubble sort is o(1).

In general sorting algorithms have space complexity O(1). This is a good thing

How to waste more memory would be if you would copy your input to another array. The time complexity is the same, you only add O(N), but now, you need O(n) memory because you need to allocate space for each position.

For example, for heapsort, you require to build a heap. In this case you need to use more memory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM