質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Q&A

0回答

508閲覧

javaでTimSortをしたい

kjfnfljnf

総合スコア23

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

0グッド

0クリップ

投稿2018/10/21 23:33

編集2018/10/22 04:04

javaでTimSortアルゴリズムのプログラムを書いています。
insertionSortメソッドとmergeSortメソッドが正しく動くことは確認できていて、
timSortメソッド内で再帰プログラムを書きたいのですが、どのようなコードを書けば良いかわかりません。

insertionSort、またはmergeSortメソッドに
配列を入れると、正しくソートはしてくれますが、要素が多いと動きがとてつもなく遅くなります。
そこで、その2つのメソッドを利用したTimSortアルゴリズムを書きたいのですが、どのタイミングで何をどのメソッドに入れれば良いかが全く分かりません。

配列の長さが100だとすると、それを、50、25、12、という風に長さが10以下になるまではmergeSortを、
それ以降はinsertionSortを利用したいです。

java

1 2public class Sorting 3{ 4 /////////////////////////////////////////////// 5 // STEP 1 -- Make sorting methods generic 6 /////////////////////////////////////////////// 7 8 /** 9 * Re-orders the contents given array using the insertion sort algorithm. 10 * 11 * @param <E> Generic to store the different values. 12 * @param data The array to be sorted. 13 */ 14 public static <E extends Comparable<E>> void insertionSort(E[] data) 15 { 16 E insert; // temporary variable to hold element to insert 17 18 // loop over data.length - 1 elements 19 for (int next = 0; next < data.length; next++) 20 { 21 insert = data[ next ]; // store value in current element 22 int moveItem = next; // initialize location to place element 23 24 while (moveItem > 0 && data[ moveItem - 1 ].compareTo(insert) > 0) 25 { 26 data[ moveItem ] = data[ moveItem - 1 ]; 27 moveItem--; 28 } 29 30 data[ moveItem ] = insert; // place inserted element 31 } 32 } 33 34 /** 35 * Sorts passed data using Merge Sort by modifying the input array. 36 * 37 * @param <E> Generic to store the different values. 38 * @param data The data to be sorted. 39 */ 40 public static <E extends Comparable<E>> void mergeSort(E[] data) 41 { 42 mergeSortHelper(data, 0, data.length - 1); 43 } 44 45 /** 46 * Recursive helper method for mergeSort. 47 * 48 * @param <E> Generic to store the different values. 49 * @param data The data in which the sub-array is located 50 * @param left Lower index of the sub-array. 51 * @param right Upper index of the sub-array. 52 */ 53 private static <E extends Comparable<E>> void mergeSortHelper(E[] data, int left, int right) 54 { 55 //General Case: The sublist has at least one item in it. 56 if ((right - left) >= 1) 57 { 58 int middle1 = (left + right) / 2; 59 int middle2 = middle1 + 1; 60 61 mergeSortHelper(data, left, middle1); 62 mergeSortHelper(data, middle2, right); 63 64 merge(data, left, middle1, middle2, right); 65 } 66 } 67 68 /** 69 * Helper method for merge sort. Merges two sub-arrays into sorted order. 70 * 71 * @param <E> Generic to store the different values. 72 * @param data The data in which the sub-arrays are located 73 * @param left Lower index of first sub-array. 74 * @param middle1 Upper index of first sub-array. 75 * @param middle2 Lower index of second sub-array. 76 * @param right Upper index of second sub-array. 77 */ 78 @SuppressWarnings("unchecked") 79 private static <E extends Comparable<E>> void merge(E[] data, int left, int middle1, int middle2, int right) 80 { 81 int leftIndex = left; // Local variables for tracking left and right 82 int rightIndex = middle2; // while merging. 83 84 Object[] combined = new Object[data.length]; 85 86 int combinedIndex = left; 87 while (leftIndex <= middle1 && rightIndex <= right) 88 { 89 // Is the first item of the left subarray smaller? 90 if (data[leftIndex].compareTo(data[rightIndex]) <= 0) 91 { 92 combined[combinedIndex++] = data[leftIndex++]; 93 } 94 // Or is the first item in the right one smaller? 95 else 96 { 97 combined[combinedIndex++] = (E) (data[rightIndex++]); 98 } 99 } 100 101 if (leftIndex == middle2) 102 { 103 while (rightIndex <= right) 104 { 105 combined[combinedIndex++] = data[rightIndex++]; 106 } 107 } 108 else 109 { 110 while (leftIndex <= middle1) 111 { 112 combined[combinedIndex++] = data[leftIndex++]; 113 } 114 } 115 for (int i = left; i <= right; i++) 116 { 117 data[i] = (E) (combined[i]); 118 } 119 } 120 121 122 /////////////////////////////////////////////////////// 123 // STEP 2 - Refactor Insertion Sort 124 // 125 //Write the helper method described below and then 126 // simplify insertionSort to eliminate duplicate code 127 /////////////////////////////////////////////////////// 128 129 /** 130 * insertionSortRange is a generic method that allows for 131 * sorting a sub-range of Comparable array values using the 132 * insertion sort algorithm. 133 * 134 * Ranges are specified with the parameters left and right, 135 * which are inclusive. 136 * 137 * @param <E> Generic to store the different values. 138 * @param data The array of data to sort 139 * @param left The index of the left-most position to sort 140 * @param right The index of the right most position to sort 141 */ 142 //Write the method header and body for insertionSortRange here 143 public static <E extends Comparable<E>> void insertionSortRange(E[] data, int left, int right) 144 { 145 E insert; // temporary variable to hold element to insert 146 int leftPosi = left - 1; 147 int rightPosi = right + 1; 148 // loop over data.length - 1 elements 149 for (int next = leftPosi; next < rightPosi; next++) 150 { 151 insert = data[ next ]; // store value in current element 152 int moveItem = next; // initialize location to place element 153 154 // shift items in the sorted part of the array to make room for next element 155 // making sure we don't step off the front of the array 156 while (moveItem > leftPosi && data[ moveItem - 1 ].compareTo(insert) > 0) 157 { 158 data[ moveItem ] = data[ moveItem - 1 ]; // shift element right one slot 159 moveItem--; 160 } 161 162 data[ moveItem ] = insert; // place inserted element 163 } 164 } 165 166 167 ////////////////////////////////////////////////////// 168 // STEP 3 - Complete TimSort 169 ////////////////////////////////////////////////////// 170 171 /** 172 * timSort is a generic sorting method that sorts an array of Comparable 173 * data using the TimSort algorithm. Make sure this method is public so 174 * that we can test it. 175 * 176 * @param <E> Generic to store the different values. 177 * @param data The array of data to be sorted 178 */ 179 //Write the method header for timSort here. Just uncomment the body, do not edit it. 180 181 public static <E extends Comparable<E>> void timSort(E[] data) 182 { 183 timSortHelper(data, 0, data.length - 1); 184 185 } 186 187 188 /** 189 * timSortHelper is a generic sorting method that sorts a sub-array array of Comparable 190 * data using the TimSort algorithm. This method should be public for testing purposes 191 * but would normally be private. 192 * 193 * Ranges are specified with the parameters left and right, 194 * which are inclusive. 195 * 196 * @param <E> Generic to store the different values. 197 * @param data The array of data to sort 198 * @param left The index of the left-most position to sort 199 * @param right The index of the right most position to sort 200 */ 201 public static <E extends Comparable<E>> void timSortHelper(E[] data, int left, int right) 202 { 203 //このメソッド内の仕事が分かりません。 204 } 205} 206 207

timSortHelper内をどのように書けば良いのでしょうか。
よろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

dice142

2018/10/22 02:46

「どのように書けばよいでしょうか」との質問ですが、ご自身でお書きになってますよね? どうなってほしいか、現状どうなっているのかを記載されると回答が得られやすいかと思います。
dice142

2018/10/22 03:57

いや、コードを消すのではなくて、書いてみたコードでどうなるのかとか、結果はどうなって欲しいかとかそういう情報を追記していただきたかったのですが。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問