This repository was archived by the owner on May 11, 2025. It is now read-only.
forked from processing/processing-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSort.java
More file actions
68 lines (51 loc) · 1.71 KB
/
Sort.java
File metadata and controls
68 lines (51 loc) · 1.71 KB
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
64
65
66
67
68
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2013-16 The Processing Foundation
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation, version 2.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
*/
package processing.data;
/**
* Internal sorter used by several data classes.
* Advanced users only, not official API.
*/
public abstract class Sort implements Runnable {
public Sort() { }
public void run() {
int c = size();
if (c > 1) {
sort(0, c - 1);
}
}
protected void sort(int i, int j) {
int pivotIndex = (i+j)/2;
swap(pivotIndex, j);
int k = partition(i-1, j);
swap(k, j);
if ((k-i) > 1) sort(i, k-1);
if ((j-k) > 1) sort(k+1, j);
}
protected int partition(int left, int right) {
int pivot = right;
do {
while (compare(++left, pivot) < 0) { }
while ((right != 0) && (compare(--right, pivot) > 0)) { }
swap(left, right);
} while (left < right);
swap(left, right);
return left;
}
abstract public int size();
abstract public float compare(int a, int b);
abstract public void swap(int a, int b);
}