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<iostream>
void printarr(int *arr,const int siz){
for(int i = 0;i < siz;i++) std::cout << arr[i] << " ";
std::cout << "\n";
}
int main(){
const int siz = 6;
int arr[siz] = {4,6,3,1,3,8};
std::cout << "Before sort\n";
printarr(arr,siz);
for(int i = 0;i < siz;i++){
for(int j = i+1;j < siz;j++){
if(arr[i] > arr[j]){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
std::cout << "After sort\n";
printarr(arr,siz);
}
```
std::sortwould be much better since the compiler knows the input type and can optimize accordingly \$\endgroup\$O(n)(beating all other forms). Bubble sort also has a very very very low overhead. Making it exceptionally good for sorting small list of numbers where it regularly outperforms your more complex sorts. \$\endgroup\$