Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 407da59

Browse files
committed
Add quick sort.
1 parent d255b1f commit 407da59

File tree

4 files changed

+108
-2
lines changed

4 files changed

+108
-2
lines changed

src/App.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ class App extends React.Component {
2020
{
2121
name: "Insertion Sort",
2222
active: false
23+
},
24+
{
25+
name: "Quick Sort",
26+
active: false
2327
}
2428
]
2529
};

src/components/Sortings.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
faPause,
99
faStepForward
1010
} from "@fortawesome/free-solid-svg-icons";
11+
import QuickSort from "./sorter/QuickSort";
1112

1213
class Sortings extends React.Component {
1314
constructor(props) {
@@ -23,7 +24,6 @@ class Sortings extends React.Component {
2324
}
2425

2526
setAction(a) {
26-
console.info(a);
2727
this.setState({
2828
action: a
2929
});
@@ -82,6 +82,16 @@ class Sortings extends React.Component {
8282
/>
8383
);
8484
break;
85+
case "Quick Sort":
86+
st.push(
87+
<QuickSort
88+
key="QuickSort"
89+
sortingData={this.props.sortingData}
90+
action={this.state.action}
91+
setAction={this.setAction}
92+
/>
93+
);
94+
break;
8595
default:
8696
break;
8797
}

src/components/sorter/BubbleSort.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ class BubbleSort extends React.Component {
1818
let curRun = this.state.sortingState;
1919
let ended = false;
2020
if (curRun[0] < numbers.length - 1) {
21-
// does not belong here
2221
if (curRun[1] < numbers.length - curRun[0] - 1) {
2322
if (numbers[curRun[1]] < numbers[curRun[1] + 1]) {
2423
let temp = numbers[curRun[1]];

src/components/sorter/QuickSort.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import React from "react";
2+
import SortingAnimation from "../SortingAnimation";
3+
4+
class QuickSort extends React.Component {
5+
constructor(props) {
6+
super(props);
7+
this.state = {
8+
data: [...this.props.sortingData],
9+
sortingState: [0, -1, -1, 0, 0], // i for loop, stack left, stack right, pivot, pivot index
10+
stack: [[0, this.props.sortingData.length - 1]],
11+
ended: false
12+
};
13+
14+
this.sort = this.sort.bind(this);
15+
}
16+
17+
// swaps two values within an array
18+
swap(values, pos1, pos2) {
19+
let tmp = values[pos1];
20+
values[pos1] = values[pos2];
21+
values[pos2] = tmp;
22+
return values;
23+
}
24+
25+
//was wenn array 0 ?
26+
sort() {
27+
let numbers = this.state.data;
28+
let stack = this.state.stack;
29+
let curRun = this.state.sortingState;
30+
let ended = this.state.ended;
31+
if (!ended) {
32+
console.info("next iteration: " + curRun);
33+
// alles was kleiner als das pivot element ist auf die linke Seite und alles was größer ist auf die rechte seite
34+
if (curRun[0] < curRun[2]) {
35+
console.info("sort");
36+
if (numbers[curRun[0]] <= curRun[3]) {
37+
numbers = this.swap(numbers, curRun[0], curRun[4]);
38+
curRun[4]++;
39+
}
40+
curRun[0]++;
41+
} else if (curRun[0] === curRun[2]) {
42+
// aufbauen der neu zu sortierenden Arrays
43+
console.info("build");
44+
numbers = this.swap(numbers, curRun[4], curRun[2]);
45+
if (curRun[4] - 1 > curRun[1]) {
46+
stack.push([curRun[1], curRun[4] - 1]);
47+
}
48+
if (curRun[4] + 1 < curRun[2]) {
49+
stack.push([curRun[4] + 1, curRun[2]]);
50+
}
51+
curRun[0]++;
52+
} else {
53+
// initialisieren der parameter für sortierdurchlauf
54+
let current = stack.pop();
55+
if (current === undefined) {
56+
console.info("ended");
57+
ended = true;
58+
} else {
59+
console.info(
60+
"next initial: left:" + current[0] + " , right:" + current[1]
61+
);
62+
curRun[1] = current[0];
63+
curRun[2] = current[1];
64+
curRun[3] = numbers[curRun[2]];
65+
curRun[4] = curRun[1];
66+
curRun[0] = curRun[1];
67+
}
68+
}
69+
}
70+
this.setState({
71+
data: numbers,
72+
sortingState: curRun,
73+
stack: stack,
74+
ended: ended
75+
});
76+
}
77+
78+
render() {
79+
return (
80+
<div className="animation">
81+
<SortingAnimation
82+
data={this.state.data}
83+
ended={this.state.ended}
84+
sort={this.sort}
85+
action={this.props.action}
86+
setAction={this.props.setAction}
87+
/>
88+
</div>
89+
);
90+
}
91+
}
92+
93+
export default QuickSort;

0 commit comments

Comments
 (0)