repl.it
@Wilmor21/

Search and Sort

Java

No description

fork
loading
Files
  • Main.java
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// format is Array Length | Selection Time | Insertion Time | Merge Time | Sequential Time | Binary Time

// 100 | 80866 | 58141 | 91000 | 5825 | 2330
// 200 | 295532 | 210332 | 223352 | 7851 | 3935 
// 400 | 1146253 | 686383 | 629915 | 9151 | 5490
// 800 | 1939729 | 2393508 | 1633265 | 13825 | 12633


import java.io.*;
import java.util.*;
public class Main {

  public static void main(String[] args) {
    int[] arr = new int[800];
    for(int i = 0; i <  arr.length; i++) {
      arr[i] = (int)(Math.random() * 100);
    }

	int[] selectionArr = arr.clone();
	int[] insertionArr = arr.clone();
	int[] mergeArr = arr.clone();


	long startSelectionTime = System.nanoTime();
	selectionSort(selectionArr);
	long endOfSelectionSort = System.nanoTime();
	System.out.println("Speed of selectionSort "+ (endOfSelectionSort-startSelectionTime));

	System.out.println("\n");

	long startInsertionTime = System.nanoTime();
	insertionSort(insertionArr);
	long endOfInsertionSort = System.nanoTime();
	System.out.println("Speed of insertionSort "+ (endOfInsertionSort-startInsertionTime));

	System.out.println("\n");

	long startMergeTime = System.nanoTime();
	mergesort(mergeArr, 0, mergeArr.length - 1);
	long endOfMergeSort = System.nanoTime();
	System.out.println("Speed of mergeSort "+ (endOfMergeSort-startMergeTime));
	
	System.out.println("\n");

	long startLinearSearch = System.nanoTime();
	linearSearch(mergeArr, mergeArr.length - 1);
	long endLinearSearch = System.nanoTime();
	System.out.println("Speed of linear search " + (endLinearSearch-startLinearSearch));

	long startBinarySearch = System.nanoTime();
	linearSearch(mergeArr, mergeArr.length - 1);
	long endBinarySearch = System.nanoTime();
	System.out.println("Speed of binary search " + (endBinarySearch-startBinarySearch));
  }

  public static void insertionSort(int arr[]) {
    int N = arr.length;
    for(int i = 1; i < N; i++) {
      int j = i - 1;
      int temp = arr[i];
      while(j >= 0 && temp < arr[j]) {
        arr[j + 1] = arr[j];
        j--;;
      }
      arr[j + 1] = temp;
    }
  }

  public static double linearSearch(int[] array, int key) {
    for(int i=0;i<array.length;i++){    
        if(array[i] == key){    
            return (double) i;    
        }   
    }
    return -1; 
  }
  
  public static int binarySearch(int[] array, int left, int right, int key) {
    if (left > right) {
      return -1;
    }

    int mid = (left + right) / 2;

    if (array[mid] == key) {
      return mid;
    }

    if (array[mid] > key) {
      return binarySearch(array, left, mid - 1, key);
    }

    return binarySearch(array, mid + 1, right, key);
  }

  public static void selectionSort(int[] arr){  
        for (int i = 0; i < arr.length - 1; i++)  
        {  
            int index = i;  
            for (int j = i + 1; j < arr.length; j++){  
                if (arr[j] < arr[index]){  
                    index = j;
                }  
            }  
            int smallerNumber = arr[index];   
            arr[index] = arr[i];  
            arr[i] = smallerNumber;  
        }  
    }

	private static void mergesort(int[] arr, int i, int j) {
    int mid = 0;

    if(i < j) {
      mid = (i + j) / 2;
      mergesort(arr, i, mid);
      mergesort(arr, mid + 1, j);
      merge(arr, i, mid, j);
    }
  }

	private static void merge(int[] arr, int i, int mid, int j) {
    int temp[] = new int[arr.length];
    int l = i;
    int r = j;
    int m = mid + 1;
    int k = l;

    while(l <= mid && m <= r) {
      if(arr[l] <= arr[m]) {
        temp[k++] = arr[l++];
      }
      else {
        temp[k++] = arr[m++];
      }
    }

    while(l <= mid)
      temp[k++] = arr[l++];

    while(m <= r) {
      temp[k++] = arr[m++];
    }

    for(int i1 = i; i1 <= j; i1++) {
      arr[i1] = temp[i1];
    }
  }  
}