CSE 100 Lecture Notes - Lecture 42: Bubble Sort

84 views2 pages
Lecture 41
Start preparing for Exam 4
Will act as final (no cumulative final for class)
Mostly Chapter 9
Practice Labs and assignments for sample code
Example of sorting code (going over it in class)
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> intVec;
//Store 5 inputs to intVec
for(int i = 0; i< 5; i++)
{
cout<<"Please enter the value of #"<<i+1<<": ";
int input;
cin>>input;
intVec.push_back(input);
}
cout<<endl;
//Using bubble sort algorithm to sort the vector
boolean flag = false;
do{
flag = false;
for(int j=0; j<intVec.size()-1; j++)
{
if(intVec[j]>intVec[j+1])
{
int temp = intVec[j];
intVec[j] = intVec[j+1];
intVec[j+1] = temp;
flag = true;
}
}
}while(flag);
//Display the sorted vector
Unlock document

This preview shows half of the first page of the document.
Unlock all 2 pages and 3 million more documents.

Already have an account? Log in

Get access

Grade+
$40 USD/m
Billed monthly
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
10 Verified Answers
Class+
$30 USD/m
Billed monthly
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
7 Verified Answers

Related Documents