前提・実現したいこと
バブルソート、クイックソートのスレッドを実行するプログラムを作成したいです。
データはランダムに生成してそれを並び替えようとしています。
発生している問題・エラーメッセージ
バブルソートもクイックソートもスレッド終了のメッセージが出てきません
クイックの方は
int n2[] = quickSort(n);
に対して、未定義だとエラーが出ています。
どう直せばいいのでしょうか??
該当のソースコード
import java.io.*; import java.util.Random; import java.util.LinkedList; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.LinkedList; class babble extends Thread{ //コンストラクタ babble(int[] n){ //バブル System.out.println("バブルソートスタート"); } public void run(int[] n){ int count=0; for (int j = 0; j < n.length; j++) { for(int k = n.length-1; k>j; k-- ){ if (n[k - 1] > n[k]) { int t = n[k - 1]; n[k - 1] = n[k]; n[k] = t; count++; } } } System.out.println("バブルソート終了"); for(int f = 0; f< n.length;f++){ System.out.printf("%d ",n[f]); } } } class quick extends Thread{ quick(int[] n){ System.out.println("クイックソートスタート"); } public void run(int[] n){ //コンストラクタ LinkedList<Integer> list = new LinkedList<>(); n = new int[list.size()]; //listに格納したデータを配列dataに格納 for(int y = 0; y < list.size(); y++){ n[y] = list.get(y); } //クイック int n2[] = quickSort(n); System.out.println("クイックソート終了"); } } public class sort { public static void main(String[] args){ try{ System.out.print("発生するデータ数:"); BufferedReader a = new BufferedReader (new InputStreamReader(System.in)); String str; //標準入力から読み込んだ文字列を格納 str = a.readLine(); int x = Integer.parseInt(str); //実数に変換 //Randomクラスの生成 Random rnd = new Random(); int[] n = new int[x]; for (int i=0; i<n.length; i++){ n[i] = i/100+1; } //シャッフル for(int i = n.length-1; i>0; i--){ int index = (int)(Math.random()*(i+1)); if (index == i) continue; int temp = n[i]; n[i] = n[index]; n[index] = temp; } babble b = new babble(n); b.start(); quick q = new quick(n); q.start(); b.join(); q.join(); }catch(InterruptedException | IOException e){ System.out.println(e); } } public static int[] quickSort(int[] n){ //クイックソート return quick(n, 0, n.length-1); } private static int[] quick(int[] n, int left, int right){ int[] array = n; int currentLeft = left; int currentRight = right; //要素数が1以下の時はなにもせずに返却 if (array.length < 2){ return array; } int pivot = array[(currentLeft + currentRight) / 2]; do{ while(array[currentLeft] < pivot){ currentLeft++; } while(array[currentRight] > pivot){ currentRight--; } if(currentLeft <= currentRight){ int index1 = currentLeft++; int index2 = currentRight--; int temp = array[index1]; array[index1] = array[index2]; array[index2] = temp; } }while(currentLeft <= currentRight); if(left < currentRight){ quick(array, left, currentRight); } if(currentLeft < right){ quick(array, currentLeft, right); } return array; } } ```java
回答2件
あなたの回答
tips
プレビュー