0

sorting

how to sort(using bubble sort) numbers from smallest to biggest in c++ by for nested in a easy way?

18th Aug 2018, 1:16 PM
Motahareh
Motahareh - avatar
2 Answers
+ 4
#include <iostream> using namespace std; int main() { int arr[] = {5, 2 , 1 , 7 , 9 , 3, 6}; int size = sizeof(arr)/sizeof(arr[0]); for(int i = 0; i < size; i++) { for(int j = 0; j < size - 1; j++) { if(arr[j] > arr[j+1]) { int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } for(int i = 0; i < size; i++) cout << arr[i] << " "; return 0; }
18th Aug 2018, 4:21 PM
blACk sh4d0w
blACk sh4d0w - avatar
18th Aug 2018, 1:30 PM
Disvolviĝo;
Disvolviĝo; - avatar