Skip to content

Commit 2ac0433

Browse files
committed
Sorting Algorithms chapter added
1 parent 5e8b47d commit 2ac0433

File tree

2 files changed

+178
-0
lines changed

2 files changed

+178
-0
lines changed

12-chapter-Sorting-Algorithms.js

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
class GenericArray {
2+
constructor(dataStore = [], numElements = 0, gaps = [5,3,1]) {
3+
this.dataStore = dataStore;
4+
this.numElements = numElements;
5+
this.pos = 0;
6+
this.gaps = gaps;
7+
}
8+
9+
setData() {
10+
for (let i = 0; i < this.numElements; i++) {
11+
this.dataStore[i] = Math.floor(Math.random() * (this.numElements + 1));
12+
}
13+
}
14+
15+
setGaps(gaps) {
16+
this.gaps = gaps;
17+
}
18+
19+
toString() {
20+
let values = '';
21+
for (let i = 0; i < this.numElements; i++) {
22+
values += `${this.dataStore[i]} `;
23+
if(i > 0 && i % 10 === 0) {
24+
values += '\n';
25+
}
26+
}
27+
return values;
28+
}
29+
30+
swap(arr, v1, v2) {
31+
let temp = arr[v1];
32+
arr[v1] = arr[v2];
33+
arr[v2] = temp;
34+
}
35+
36+
// The slowest sort algorithm
37+
bubbleSort() {
38+
let numElements = this.dataStore.length;
39+
for (let outer = numElements; outer >= 2; outer--) {
40+
for (let inner = 0; inner <= outer - 1; inner++) {
41+
if(this.dataStore[inner] > this.dataStore[inner+1]) {
42+
this.swap(this.dataStore, inner, inner+1);
43+
}
44+
}
45+
}
46+
}
47+
48+
// faster sort algorithm
49+
selectionSort() {
50+
let min;
51+
for (let outer = 0; outer <= this.dataStore.length-2; outer++) {
52+
min = outer;
53+
for (let inner = outer + 1; inner <= this.dataStore.length-1; inner++) {
54+
if (this.dataStore[inner] < this.dataStore[min]) {
55+
min = inner;
56+
}
57+
}
58+
this.swap(this.dataStore, outer, min);
59+
}
60+
}
61+
62+
// fastest sort algorithm for small data sets
63+
insertionSort() {
64+
var temp, inner;
65+
for (let outer = 1; outer <= this.dataStore.length-1; outer++) {
66+
temp = this.dataStore[outer];
67+
inner = outer;
68+
while (inner > 0 && (this.dataStore[inner-1] >= temp)) {
69+
this.dataStore[inner] = this.dataStore[inner-1];
70+
--inner;
71+
}
72+
this.dataStore[inner] = temp;
73+
}
74+
}
75+
76+
// Advanced sort algorithms
77+
shellSort() {
78+
for (let g = 0; g < this.gaps.length; g++) {
79+
for (let i = this.gaps[g]; i < this.dataStore.length; i++) {
80+
let temp = this.dataStore[i];
81+
let last;
82+
for (let j = i; j >= this.gaps[g] && this.dataStore[j - this.gaps[g]] > temp; j -= this.gaps[g]) {
83+
this.dataStore[j] = this.dataStore[j - this.gaps[g]];
84+
last = j;
85+
}
86+
this.dataStore[last] - temp;
87+
}
88+
}
89+
}
90+
91+
// The king for sorting large data sets
92+
quickSort(alist) {
93+
this.quickSortHelper(alist,0,alist.length-1);
94+
}
95+
96+
quickSortHelper(alist,first,last) {
97+
if(first < last) {
98+
const splitpoint = this.partition(alist,first,last);
99+
100+
this.quickSortHelper(alist,first,splitpoint-1);
101+
this.quickSortHelper(alist,splitpoint+1,last);
102+
}
103+
}
104+
105+
partition(alist,first,last) {
106+
let pivotvalue = alist[first];
107+
108+
let leftmark = first+1;
109+
let rightmark = last;
110+
let done = false;
111+
112+
while (!done) {
113+
while (leftmark <= rightmark && alist[leftmark] <= pivotvalue) {
114+
leftmark = leftmark + 1;
115+
}
116+
117+
while (alist[rightmark] >= pivotvalue && rightmark >= leftmark) {
118+
rightmark = rightmark -1;
119+
}
120+
121+
if (rightmark < leftmark) {
122+
done = true;
123+
} else {
124+
this.swap(alist, leftmark, rightmark);
125+
}
126+
}
127+
128+
let temp = alist[first];
129+
alist[first] = alist[rightmark];
130+
alist[rightmark] = temp;
131+
132+
return rightmark;
133+
}
134+
135+
}
136+
137+
// Implementation
138+
// ####################################################
139+
function displayImplementations(obj, typeSort) {
140+
console.log(typeSort);
141+
// console.log(obj.toString());
142+
let start = new Date().getTime();
143+
144+
switch (typeSort) {
145+
case 'Bubble Sort':
146+
myNums.bubbleSort();
147+
break;
148+
case 'Selection Sort':
149+
myNums.selectionSort();
150+
break;
151+
case 'Insertion Sort':
152+
myNums.insertionSort();
153+
break;
154+
case 'Shell Sort':
155+
myNums.shellSort();
156+
break;
157+
case 'Quick Sort':
158+
myNums.quickSort(myNums.dataStore);
159+
break;
160+
default:
161+
myNums.dataStore.sort();
162+
}
163+
164+
let end = new Date().getTime();
165+
console.log();
166+
// console.log(myNums.toString());
167+
console.log(`Elapsed time for the ${typeSort} on ${numElements} elements is: ${end} - ${start} = ${end - start} milliseconds.`);
168+
console.log('###################');
169+
}
170+
171+
const sortTypes = ['Bubble Sort', 'Selection Sort', 'Insertion Sort', 'Shell Sort', 'Quick Sort','default'];
172+
const numElements = 10000;
173+
const myNums = new GenericArray(undefined, numElements);
174+
for (let type in sortTypes) {
175+
myNums.setData();
176+
displayImplementations(myNums, sortTypes[type]);
177+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ The purpose of this repo is to update the exercises to ES6 standards and to corr
2424
| 11.- | [Binary Trees and Binary Search Trees](./10-chapter-Binary-Trees.js) | 4 | `Binary trees` are chosen over other more primary data structures because you can search a binary tree very quickly (as opposed to a linked list, for example) and you can quickly insert and delete data from a binary tree (as opposed to an array).
2525
| 12.- | [Simple Graphs](./11-chapter-1-MatrixAdjencency-Graphs.js) | 2 | A graph consists of a set of vertices and a set of edges. A map is a type of graph where each town is a vertex, and a road that connects two towns is an edge. Edges are defined as a pair (v1, v2), where v1 and v2 are two vertices in a graph
2626
| 13.- | [Complete Graphs](./11-chapter-2-ArrayListAdjecency-Graphs.js) | 2 | A complete graph is where all vertices are conected with each other.
27+
| 14.- | [Sorting Algorithms](./12-chapter-Sorting-Algorithms.js) | 6 | Two of the most common operations performed on data stored in a computer are sorting and searching. The sorting algorithms are Bubble Sort, Selection Sort, Insertion Sort, Shell Sort and Quick Sort.
2728

2829

2930
### To run the examples we need the following:

0 commit comments

Comments
 (0)