简体   繁体   中英

How to reset a sorted array to unsorted in Java?

I am writing a program to compare different sort methods. I random generated 100,000 integers and store these data to an array. I want to apply the same array to different sort methods in order to do the comparison. (I think create class for each method may solve my problem. But I don't want to create too many class). So I decided to create one class called Sorts and bunch of sort functions under this class. I want to my sorted array reset to unsorted in order apply the same array to different sort method. Could anyone tell me how?

Generate data:

int size = Integer.parseInt(br.readLine());
    int [] data = new int[size];
    for (int i = 0; i< size; i++){
        data[i] = (int)(Math.random()*(10*size));
        System.out.print(data[i]+" ");
    }

Create object:

Sorts sort = new Sorts(size, data);

Invoke:

switch(index){
            case "1" :
                System.out.print("\nYou select bubble sort\n");
                sort.bubbleSort();
                break;
            case "2" :
                System.out.print("You select quick sort\n");
                sort.quickSort();
                break;

My class:

class Sorts{
  private int size;
  private int data[];
  Sorts(int size, int [] data){
      this.size = size;
      this.data = data;
  }

  protected void bubbleSort(){
      int temp = 0;
      for (int i = 0; i< (data.length-1); i++){
          for (int j = 0; j<(data.length-1);j++){
              if(data[j]>data[j+1]){
                  temp = data[j];
                  data[j] = data[j+1];
                  data[j+1]= temp;
              }
          }
      }
      printResult();

  }


  protected void quickSort(){

  }

  protected void resetData(){

  }
}

Well, you could use Collections.shuffle , which takes a 'List' parameter. This would obviously mean converting your 'int[]' to 'List' (and back again).

On another point, rather than implement it as you have, Ithink it's better to use the Strategy pattern to implement multiple sort mechanisms like this. For instance:

interface Sort {
    void sort(int data[], int size);
}

class QuickSort implements Sort {
    void sort(int data[], int size) {
        ...
    }
}    

class MergeSort implements Sort {
    void sort(int data[], int size) {
        ...
    }
}

etc...

As a further aside:

  • this is only sorting ints so consider how to sort any type (and perhaps generics).
  • some sorts (eg MergeSort) are stable and use new arrays to represent the sorted data. How can you return that to the caller? You can't set 'data' to your new array.

I think for the usecase OP seems to have, the array just needs a reshuffling, this method reshuffles although there is no guarantee that it would reset it to the original array

private void reset(final int arr[]) {
    Random rand = new Random();

    for (int i = 0; i < arr.length; i++) {
         int nextInt = rand.nextInt(arr.length);
         int temp = arr[i];
         arr[i] = arr[nextInt];
         arr[nextInt] = temp;
    }
}

You can use the Method shuffle of the class Collection

Collection.shuffle(yourList);

This will shuffle your list automatically without implementing an own function. After that you can convert the list back to an array. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM