Inspiration -MLH

//in c++
//problem statement: Write a Sorting Method Writing code to sort a list is another classic computer science problem. Using any language you’d like, sort a list!

include

include

void bubbleSort(std::vector &arr) { int n = arr.size(); bool swapped; for (int i = 0; i < n - 1; ++i) { swapped = false; for (int j = 0; j < n - i - 1; ++j) { if (arr[j] > arr[j + 1]) { std::swap(arr[j], arr[j + 1]); swapped = true; } } // If no two elements were swapped in the inner loop, array is sorted if (!swapped) { break; } } }

int main() { std::vector myVec = {64, 34, 25, 12, 22, 11, 90};

std::cout << "Original array: ";
for (int num : myVec) {
    std::cout << num << " ";
}
std::cout << std::endl;

bubbleSort(myVec);

std::cout << "Sorted array: ";
for (int num : myVec) {
    std::cout << num << " ";
}
std::cout << std::endl;

return 0;

}

Built With

Share this project:

Updates