Sorting an array of integers in C++ This is a follow up to my previous post as it contained a lot of errors and wasn't accurate.
I am using the Bubble sort algorithm to sort an array of integers and I would like to know whether I could optimize it in any way.
#include&lt;iostream&gt;


void printarr(int *arr,const int siz){
    for(int i = 0;i &lt; siz;i++) std::cout &lt;&lt; arr[i] &lt;&lt; &quot; &quot;;
    std::cout &lt;&lt; &quot;\n&quot;;
}


int main(){
    const int siz = 6;
    int arr[siz] = {4,6,3,1,3,8};
    std::cout &lt;&lt; &quot;Before sort\n&quot;;
    printarr(arr,siz);
    for(int i = 0;i &lt; siz;i++){
        for(int j = i+1;j &lt; siz;j++){
            if(arr[i] &gt; arr[j]){
                int temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
    std::cout &lt;&lt; &quot;After sort\n&quot;;
    printarr(arr,siz);
}
```
First thing first, make it into a function, such that your main will be
int main(){
    const int siz = 6;
    int arr[siz] = {4,6,3,1,3,8};

    std::cout &lt;&lt; &quot;Before sort\n&quot;;
    printarr(arr,siz);

    sort(arr, siz);

    std::cout &lt;&lt; &quot;After sort\n&quot;;
    printarr(arr,siz);
}


Since you've tagged it c++, do not use c-style array. Use std::vector. Use std::swap. Currently, it is plain old c. Nothing wrong with it, except the tag.

Optimization wise, well, bubble sort does not deserve it.