Description
Input a sequence of numbers. Please insert them into a vector, and sort them in ascenind order(ASC) and in descending order(DESC) by bubble sort and sort() in stl repectively.
Complete below three functions and two existed class BubbleSort, STLSort.
void InputVector(vector<int> &vec, int n) – stdin n numbers and insert them into vec from back.
void CopyVector(vector<int> &vec, vector<int> &other) – copy all elements in vec and replace elements in other with them.
note: vec and other would own same elements after this function.
For example:
vec = {1, 3, 5, 7} other = {2, 4, 6, 8, 10}
after CopyVector(vec, other)
vec = {1, 3, 5, 7} other = {1, 3, 5, 7}
void PrintVector(vector<int> &vec) – print out all elements in vec in order.
note: need print return value at the end.
BubbleSort
Private Methods:
- void swap(int *ptr1, int *ptr2) – Swap the value pointed by ptr1 and the value pointed by ptr2
Public Methods:
- void sort(vector<int> &vec, bool ascending = true) – Sort elements in vec. Sort in ascending order if ascending is true; otherwise, sort in descending order. (no need implement)
STLSort
Public Methods:
- void sort(vector<int> &vec, bool ascending = true) – Sort elements in vec. Sort in ascending order if ascending is true; otherwise, sort in descending order.
note: use STL sort, it’s super easy.
function.cpp: download
#include "function.h"
#include <bits/stdc++.h>
using namespace std;
/* Vector */
void InputVector(vector<int> &vec, int n)
{
// Todo
}
void CopyVector(vector<int> &vec, vector<int> &other)
{
// Todo
}
void PrintVector(vector<int> &vec)
{
// Todo
// Note: Remember to print '\n' at the end
}
/* Bubble Sort */
void BubbleSort::swap(int *ptr1, int *ptr2)
{
// Todo
}
void BubbleSort::sort(vector<int> &vec, bool ascending)
{
if (vec.size() < 2)
return;
for (int i = vec.size(); i > 1; i--)
{
for (int j = 0; j < i - 1; j++)
{
if (ascending)
{
if (vec[j] > vec[j + 1])
swap(&vec[j], &vec[j + 1]);
}
else
{
if (vec[j] < vec[j + 1])
swap(&vec[j], &vec[j + 1]);
}
}
}
}
/* STL Sort */
void STLSort::sort(vector<int> &vec, bool ascending)
{
if (ascending)
{
// Todo
}
else
{
// Todo
}
}
Input
n
N1 N2 N3 N4 … Nn
n represents the total number of numbers (N1, N2, N3 …) appear in the next line.
note: 20 >= n > 0. Also, numbers appear in next line are bounded in normal c++ int data type.
Output
No need to handle output. Its shown in main.cpp.
Partial Judge Code
13073.cpp
Partial Judge Header
13073.h
Tags