-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubble.h
63 lines (59 loc) · 1.57 KB
/
bubble.h
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
54
55
56
57
58
59
60
61
62
63
// =================================================================
//
// File: bubble.h
// Author: Pedro Perez
// Description: This file contains the implementation of the
// bubble sort.
//
// Copyright (c) 2020 by Tecnologico de Monterrey.
// All Rights Reserved. May be reproduced for any non-commercial
// purpose.
// =================================================================
#ifndef BUBBLE_H
#define BUBBLE_H
#include "header.h"
#include <vector>
// =================================================================
// Performs the bubble sort algorithm on an array
//
// @param A, an array of T elements.
// @param size, the number of elements in the array.
// =================================================================
template <class T>
int bubbleSort(T *arr, int size) {
int counter = 0;
for(int i = size - 1; i > 0; i--){
for(int j = 0; j < i; j++){
if(arr[j] > arr[j + 1]){
swap(arr, j, j + 1);
counter++;
}
}
}
return counter;
}
// =================================================================
// Performs the bubble sort algorithm on an vector.
//
// @param v, a vector of T elements.
// =================================================================
template <class T>
int bubbleSort(std::vector<T> &v) {
vector <int> a = v;
int counter = 0;
bool bandera = false;
for(int i = 0; i<a.size(); i++){
for(int j = 0; j<a.size()-1; j++){
if(a[j+1] < a[j]){
counter++;
swap(a[j+1],a[j]);
bandera = true;
}
}
if(bandera = false){
break;
}
}
return counter;
}
#endif /* BUBBLE_H */