-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSortingFunction.java
More file actions
183 lines (166 loc) · 4.13 KB
/
SortingFunction.java
File metadata and controls
183 lines (166 loc) · 4.13 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package com.bridgelabz.utility;
/*
* created by: Bridge Labz
* Date 4/05/2016
*
* Perpose: Providing functionality related to Array
1: Read string and int array.
2: Display string and int array
3: sorting algo (bubble, insertaion sort, binary serach)
4: Statement to array of word
**/
import com.bridgelabz.utility.Utility;
import java.util.StringTokenizer;
public class SortingFunction{
Utility mtility;
StringTokenizer mTok;
public SortingFunction(){
mtility=new Utility();
}
//Take 1D Array Input
public int[] input1DArray(int arraySize){
int array[]=new int[arraySize];
for(int i=0;i<arraySize;i++){
System.out.println("Enter array["+i+"] : ");
array[i]=mtility.inputInteger();
}
return array;
}
//Insertation sort for String
public String[] insertationSort4String(String words[]){
String temp;
for(int i=1;i<words.length;i++){
for(int j=i;j>0;j--){
if(words[j].compareTo(words[j-1])<0){
temp=words[j];
words[j]=words[j-1];
words[j-1]=temp;
}
else
break;
}
}
return words;
}
//Insertation sort
public int[] insertationSort(int words[]){
int temp;
for(int i=1;i<words.length;i++){
for(int j=i;j>0;j--){
if((words[j]-words[j-1])<0){
temp=words[j];
words[j]=words[j-1];
words[j-1]=temp;
}
else
break;
}
}
return words;
}
//Take 1D String Array Input
public String[] input1DStringArray(int arraySize){
String array[]=new String[arraySize];
for(int i=0;i<arraySize;i++){
System.out.println("Enter array["+i+"] : ");
array[i]=mtility.inputWord();
}
return array;
}
//Print 1D array
public void print1DArray(int array[]){
for(int i=0;i<array.length;i++)
System.out.print(array[i]+" ");
System.out.println();
System.out.println();
System.out.println();
}
//print 1D array String
public void print1DStringArray(String array[]){
for(int i=0;i<array.length;i++)
System.out.print(array[i]+" ");
System.out.println();
}
//Binary Search
public int binarySearch(int[] inputArr, int key) {
int start = 0;
int end = inputArr.length - 1;
while (start <= end) {
int mid = (start + end) / 2;
if (key == inputArr[mid]) {
return mid;
}
if (key < inputArr[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
}
return -1;
}
//Binary Search for String
public int binarySearchString(String[] names, String key) {
int first = 0;
int last = names.length;
while (first < last) {
int mid = (first + last) / 2;
if (key.compareTo(names[mid]) < 0) {
last = mid;
} else if (key.compareTo(names[mid]) > 0) {
first = mid + 1;
} else {
return mid;
}
}
return -1;
}
//Bubble Sort for Integer
public int[] bubbleSortInAscendingOrder(int numbers[])
{
int temp;
for(int i = 0; i < numbers.length; i++)
{
for(int j = 1; j < (numbers.length -i); j++)
{
//if numbers[j-1] > numbers[j], swap the elements
if(numbers[j-1] > numbers[j])
{
temp = numbers[j-1];
numbers[j-1]=numbers[j];
numbers[j]=temp;
}
}
}
return numbers;
}
//Bubble Sort for String
public String[] bubbleSortInAscendingOrderString(String numbers[])
{
String temp;
for(int i = 0; i < numbers.length; i++)
{
for(int j = 1; j < (numbers.length -i); j++)
{
//if numbers[j-1] > numbers[j], swap the elements
if(numbers[j-1].compareTo(numbers[j])<0)
{
temp = numbers[j-1];
numbers[j-1]=numbers[j];
numbers[j]=temp;
}
}
}
return numbers;
}
//Take Statement And return Array of words
public String[] wordsArrayFromStatement(String statement){
mTok=new StringTokenizer(statement," ");
String words[]=new String[mTok.countTokens()];
int i=0;
while(mTok.hasMoreTokens()){
words[i]=mTok.nextToken();
i++;
}
return words;
}
}