GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — PLM-PLD_ClassWorks/PLD_BubbleSort_stepByStep.cpp at main · proDev-Theron/PLM-PLD_ClassWorks

Expand file tree

/

PLD_BubbleSort_stepByStep.cpp

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
53 lines (48 loc) · 1.52 KB
/

PLD_BubbleSort_stepByStep.cpp

Copy path

File metadata and controls

53 lines (48 loc) · 1.52 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<iostream>
using namespace std;
/*
Created by Theron Adrianne Bueno
January 31, 2021
Subject: Programming Logic and Design (CPE 0112.1) - Pamantasan ng Lungsod ng Maynila (PLM)
Task: Demonstrate Bubble Sort
*/
int arr[] = {33, 42, 35, 50, 20, 16, 45, 26, 5, 72, 18}; //create an array with given number of elements
int n = sizeof(arr)/sizeof(arr[0]); //get array size
int stepsTaken = 0; // Variable to store the number of iterations to complete the sort
//Function for printing the elements of array nicely
void display(int *array, int size)
{
for(int i = 0; i<size; i++)
cout << array[i] << " ";
cout << endl;
}
void bubbleSort(int* array, int size)
{
bool isSorted = false;
int lastUnsorted = n-1; //Because bubble sort pushes the element with the highest value to the last index, it would be faster if the
//program won't sort anymore the sorted values
while(!isSorted)
{
isSorted = true;
for (int i = 0; i < lastUnsorted; i++)
{
if (arr[i] > arr[i+1])
{
swap(arr[i],arr[i+1]);
isSorted = false;
stepsTaken++; //Count the number of iterations
display(arr, n); //Print each iteration
}
}
lastUnsorted--;
}
}
int main()
{
cout << "Array before Sorting: ";
display(arr, n);
bubbleSort(arr, n);
cout << "\nArray after Sorting: ";
display(arr, n);
cout << "\n\nNumber of steps taken: " << stepsTaken << " \n\n";
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.