- public class SequentialSearch {
- public static void main(String[] args) {
- int n = Integer.parseInt(args[0]);
- int[] a = {50, 30, 40, 10, 20};
- int i = search(a, n);
- if (i >= 0) {
- System.out.println(n + " was found.(index = " + i + ")");
- } else {
- System.out.println(n + " was not found.");
- }
- }
- static int search(int[] a, int n) {
- for (int i = 0; i < a.length; i++) {
- if (a[i] == n) {
- return i;
- }
- }
- return -1;
- }
- }
□ 実行結果
$java SequentialSearch 50 50 was found.(index = 0) $java SequentialSearch 51 51 was not found.
- public class BinarySearch {
- public static void main(String[] args) {
- int n = Integer.parseInt(args[0]);
- int[] a = {10, 20, 30, 40, 50};
- int i = search(a, n);
- if (i >= 0) {
- System.out.println(n + " was found.(index = " + i + ")");
- } else {
- System.out.println(n + " was not found.");
- }
- }
- static int search(int[] a, int n) {
- int l = 0;
- int h = a.length - 1;
- int m;
- while (l <= h) {
- m = (l + h) / 2;
- if (a[m] < n) {
- l = m + 1;
- } else if (a[m] > n) {
- h = m - 1;
- } else {
- return m;
- }
- }
- return -1;
- }
- }
□ 実行結果
$java BinarySearch 50 50 was found.(index = 4) $java BinarySearch 51 51 was not found.
- public class Rank01 {
- public static void main(String[] args) {
- int[] a = {5, 3, 6, 5, 8, 6, 9, 1, 3, 3};
- int[] rank = new int[a.length];
- for (int i = 0; i < a.length; i++) {
- rank[i] = 1;
- for (int j = 0; j < a.length; j++) {
- if (a[j] > a[i]) {
- rank[i]++;
- }
- }
- }
- for (int i = 0; i < a.length; i++) {
- System.out.println(a[i] + " - " + rank[i]);
- }
- }
- }
□ 実行結果
$java Rank01 5 - 5 3 - 7 6 - 3 5 - 5 8 - 2 6 - 3 9 - 1 1 - 10 3 - 7 3 - 7
- public class Count01 {
- public static void main(String[] args) {
- int[] a = {2, 2, 3, 5, 5, 5, 7, 8, 8};
- int prevValue = a[0];
- int count = 0;
- for (int i = 0; i < a.length; i++) {
- if (prevValue == a[i]) {
- count++;
- } else {
- System.out.println(prevValue + " : " + count);
- count = 1;
- prevValue = a[i];
- }
- }
- System.out.println(prevValue + " : " + count);
- }
- }
□ 実行結果
$java Count01 2 : 2 3 : 1 5 : 3 7 : 1 8 : 2
- public class Matching01 {
- public static void main(String[] args) {
- int[] a = {10, 20, 30, 40, 50, Integer.MAX_VALUE};
- int[] b = {15, 20, 30, 35, 50, 60, Integer.MAX_VALUE};
- System.out.println(ArrayUtil.toString(match(a, b)));
- }
- static int[] match(int[] a, int[] b) {
- int[] c = new int[a.length + b.length];
- int i = 0, j = 0, k = 0;
- while (a[i] < Integer.MAX_VALUE || b[j] < Integer.MAX_VALUE) {
- if (a[i] < b[j]) {
- c[k++] = a[i++];
- } else if (a[i] == b[j]) {
- c[k++] = a[i++];
- j++;
- } else {
- c[k++] = b[j++];
- }
- }
- c[k] = Integer.MAX_VALUE;
- return c;
- }
- }
□ 実行結果
$java Matching01 [10, 15, 20, 30, 35, 40, 50, 60, 2147483647, 0, 0, 0, 0]
分類 | 計算時間 |
---|---|
選択ソート | O(n2) |
挿入ソート | O(n2) |
バブル・ソート | O(n2) |
クイック・ソート | O(n*log2n) |
バケット・ソート | O(n) |
基数ソート | O(n) |
- public class Sort01 {
- public static void main(String[] args) {
- int n = args.length;
- int[] a = new int[n];
- for (int i = 0; i < n; i++) {
- a[i] = Integer.parseInt(args[i]);
- }
- for (int i = 0; i < n - 1; i++) {
- for (int j = i + 1; j < n; j++) {
- if (a[i] > a[j]) {
- int t = a[i];
- a[i] = a[j];
- a[j] = t;
- }
- }
- }
- System.out.println(ArrayUtil.toString(a));
- }
- }
□ 実行結果
$java Sort01 6 3 5 1 [1, 3, 5, 6]