-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSorting-1 Assignment
More file actions
201 lines (194 loc) · 7.29 KB
/
Copy pathSorting-1 Assignment
File metadata and controls
201 lines (194 loc) · 7.29 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
Q1 - Given an array of strings arr[]. Sort given strings using Bubble Sort and display the sorted array.
import java.util.Scanner;
public class Test{
public static void main(String[] args){
Scanner scn = new Scanner(System.in);
System.out.println("Enter the length of array");
int n = scn.nextInt();
String[] arr = new String[n];
System.out.println("Enter the elements of array");
for(int i = 0; i < n; i++){
arr[i] = scn.next();
}
for (int j = 0; j < n - 1; j++){
for (int i = j + 1; i < n; i++){
if (arr[j].compareTo(arr[i]) > 0){
String temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
for(int i = 0; i < n; i++){
System.out.print(arr[i] + " ");
}
}
}
Q2- Given a string s, return the string which contains all characters of s in lexicographical order.
import java.util.Scanner;
public class Test{
public static void main(String[] args){
Scanner scn = new Scanner(System.in);
System.out.println("Enter the string: ");
String s = scn.nextLine();
char[] arr = s.toCharArray();
//insertion sort
for (int i = 1; i < arr.length; i++) {
int j = i;
// Insert a[i] into sorted left part 0..i-1
while (j > 0 && arr[j] < arr[j - 1]) {
// Swap a[j] and a[j-1]
char temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
// Decrement j by 1
j--;
}
}
String ans = "";
for(int i = 0; i < arr.length; i++){
ans += arr[i];
}
System.out.println(ans);
}
}
Q3 - Given an integer array and an integer k where k<size of array, We need to return the kth smallest element of the array.
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
System.out.println("Enter the length of array");
int n = scn.nextInt();
int[] arr = new int[n];
System.out.println("Enter the elements of array");
for (int i = 0; i < n; i++) {
arr[i] = scn.nextInt();
}
System.out.println("Enter the index");
int k = scn.nextInt();
for (int i = 1; i < n; i++) {
int j = i;
// Insert a[i] into sorted left part 0..i-1
while (j > 0 && arr[j] < arr[j - 1]) {
// Swap a[j] and a[j-1]
int temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
// Decrement j by 1
j--;
}
}
System.out.print(arr[k - 1]);
}
}
Q4 - Given an array of n elements, the task is to find the elements that are greater than half of elements in an array.
In case of odd elements, we need to print elements larger than floor(n/2) elements where n is total number of elements in array.
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
System.out.println("Enter the length of array");
int n = scn.nextInt();
int[] arr = new int[n];
System.out.println("Enter the elements of array");
for (int i = 0; i < n; i++) {
arr[i] = scn.nextInt();
}
for (int i = 1; i < n; i++) {
int j = i;
// Insert a[i] into sorted left part 0..i-1
while (j > 0 && arr[j] < arr[j - 1]) {
// Swap a[j] and a[j-1]
int temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
// Decrement j by 1
j--;
}
}
int si;
if(n % 2 == 0){
si = n/2;
}else{
si = n/2 + 1;
}
for (int i = si; i < n; i++)
System.out.print(arr[i] + " ");
}
}
Q5 - Given an array of digits (values are from 0 to 9), the task is to find the minimum possible sum of two numbers formed from digits of the array.
Please note that all digits of the given array must be used to form the two numbers.
import java.util.Scanner;
public class Test{
public static void main(String[] args){
Scanner scn = new Scanner(System.in);
System.out.println("Enter the length of the array: ");
int n = scn.nextInt();
int[] arr = new int[n];
System.out.println("Enter the elements of the array: ");
for(int i = 0; i < n; i++){
arr[i] = scn.nextInt();
}
//insertion sort
for (int i = 1; i < arr.length; i++) {
int j = i;
// Insert a[i] into sorted left part 0..i-1
while (j > 0 && arr[j] < arr[j - 1]) {
// Swap a[j] and a[j-1]
int temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
// Decrement j by 1
j--;
}
}
int a = 0;
int b = 0;
for(int i = 0; i < arr.length; i++){
if(i % 2 == 0){
a = a * 10 + arr[i];
}else{
b = b * 10 + arr[i];
}
}
System.out.println(a + b);
}
}
Q6 - Given an array with N distinct elements, convert the given array to a form where all elements are in the range from 0 to N-1. The order of elements is the same, i.e., 0 is placed in the place of the smallest
element, 1 is placed for the second smallest element, ... N-1 is placed for the largest element.
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
System.out.println("Enter the length of the array");
int n = scn.nextInt();
System.out.println("Enter the elements of the array");
int[] arr = new int[n];
for(int i = 0; i < n; i++){
arr[i] = scn.nextInt();
}
int[] temp = arr.clone();
for (int i = 1; i < n; i++) {
int j = i;
while (j > 0 && temp[j] < temp[j - 1]){
int val = temp[j];
temp[j] = temp[j-1];
temp[j-1] = val;
j--;
}
}
int pos = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(arr[j] == temp[i]){
arr[j] = pos;
pos++;
break;
}
}
}
for(int i = 0; i < n; i++){
System.out.print(arr[i] + " ");
}
}
}