|
| 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