Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/example-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
- main
pull_request:
types: [opened, synchronize, reopened]
workflow_dispatch: {}
jobs:
test:
runs-on: ubuntu-22.04-arm
Expand Down
46 changes: 38 additions & 8 deletions bubble_sort.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <iostream>
#include <vector>
#include <random>

// BubbleSort sorts a vector of integers using the bubble sort algorithm
void bubbleSort(std::vector<int>& arr) {
Expand All @@ -16,20 +17,49 @@ void bubbleSort(std::vector<int>& arr) {
}
}

// SlowBubbleSort is an inefficient bubble sort that always performs all comparisons
void slowBubbleSort(std::vector<int>& arr) {
int n = arr.size();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap adjacent elements
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

int main() {
std::vector<int> numbers = {64, 34, 25, 12, 22, 11, 90};
// Generate random size between 100 and 100000
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> sizeDistr(100, 100000);
int size = sizeDistr(gen);

// Create a dynamic vector at runtime with random size
std::vector<int> dynamicVector(size);

// Fill vector with random values
std::uniform_int_distribution<> valueDistr(1, 1000);
for (int i = 0; i < size; i++) {
dynamicVector[i] = valueDistr(gen);
}

std::cout << "Original array: ";
for (int num : numbers) {
std::cout << num << " ";
std::cout << "Generated vector of size: " << size << std::endl;
std::cout << "First 10 elements: ";
for (int i = 0; i < std::min(10, size); i++) {
std::cout << dynamicVector[i] << " ";
}
std::cout << std::endl;

bubbleSort(numbers);
bubbleSort(dynamicVector);

std::cout << "Sorted array: ";
for (int num : numbers) {
std::cout << num << " ";
std::cout << "Sorted! First 10 elements: ";
for (int i = 0; i < std::min(10, size); i++) {
std::cout << dynamicVector[i] << " ";
}
std::cout << std::endl;

Expand Down
Loading