Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * @author Kyle Kemp
- *
- */
- public class Sorting {
- private static class Smallest {
- public Smallest(int first, int second){
- this.first = first;
- this.second = second;
- }
- public Smallest() {}
- private int first, second;
- public void setSmallest(int[] arr, int pos){
- if(arr[pos] < arr[first]) { first = pos; organize(arr); return; }
- if(arr[pos] < arr[second]) { second = pos; organize(arr); return; }
- }
- private void organize(int[] arr) {
- int temp = first;
- if(arr[second] < arr[first]) { first = second; second = temp; }
- }
- public int getFirst() { return first; }
- public int getSecond() { return second; }
- public void setFirst(int first) { this.first = first; }
- public void setSecond(int second) { this.second = second; }
- }
- public static void main(String[] args){
- int[][] arr = { { 1,2,3,4}, {123, 5, 0 }, {123, 1, 0} };
- int[] array = {1,2,3,4,7,12,0,6,69, 70};
- mySelectionSort(array);
- printArray(array);
- }
- public static int[] reverse(int[] arr) {
- int[] reversed = new int[arr.length];
- for(int x=0; x<arr.length; x++) {
- reversed[x] = arr[arr.length-x-1];
- }
- return reversed;
- }
- public static void sortTwoD(int[][] sort){
- //calculate the length of the 1d array to sort
- int maxSize = 0;
- for(int x=0; x<sort.length; x++){
- maxSize += sort[x].length;
- }
- int[] sortMe = new int[maxSize];
- int curPos = 0;
- //compress the array down to one dimenson
- for(int x=0; x<sort.length; x++){
- System.arraycopy(sort[x], 0, sortMe, curPos, sort[x].length);
- curPos += sort[x].length;
- }
- mySelectionSort(sortMe);
- curPos = 0;
- //rewrite the array to contain all of the sorted numbers
- for(int x=0; x<sort.length; x++){
- System.arraycopy(sortMe, curPos, sort[x], 0, sort[x].length);
- curPos += sort[x].length;
- }
- }
- private static void swap(int[] arr, int oldpos, int newpos){
- int temp = arr[oldpos];
- arr[oldpos] = arr[newpos];
- arr[newpos] = temp;
- }
- private static int findHighestPos(int[] arr) {
- int r = 0;
- for(int i=0; i<arr.length; i++) {
- if(arr[i] > arr[r]) {
- r = i;
- }
- }
- return r;
- }
- public static void mySelectionSort(int[] sort){
- int max = findHighestPos(sort);
- Smallest min = new Smallest(max,max);
- for(int pos=0; pos<sort.length; pos++) {
- for(int i=0; i<sort.length; i++) {
- min.setSmallest(sort, pos);
- }
- System.out.println("smallest: "+min.first+ " "+min.second);
- for(int i=0; i<sort.length; i++) {
- if(sort[min.first] < sort[i]) {
- swap(sort, min.first, i);
- break;
- }
- }
- for(int i=min.first; i<sort.length; i++) {
- if(sort[min.second] < sort[i]) {
- swap(sort, min.second, i);
- break;
- }
- }
- }
- printArray(sort);
- }
- public static void printArray(int[] arr){
- for(int i : arr){
- System.out.print(i + " ");
- }
- System.out.println();
- }
- public static void print2DArray(int[][] arr){
- // get the maximum size of a number, so it can be dynamically pretty
- int maxSize=0;
- for(int x=0; x<arr.length; x++){
- for(int y=0; y<arr[x].length; y++){
- maxSize = Math.max((""+arr[x][y]).length(), maxSize);
- }
- }
- for(int x=0; x<arr.length; x++){
- for(int y=0; y<arr[x].length; y++){
- System.out.format("%"+maxSize+"d,", arr[x][y]);
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement