0% found this document useful (0 votes)
5 views7 pages

Java Sorting Algorithms Implementation

The document contains a Java program that implements various sorting algorithms, including selection sort, insertion sort, and their reverse versions for both integers and strings. It allows users to input integers and strings, sorts them using the defined algorithms, and prints the results after each pass. The program demonstrates the sorting process step-by-step for better understanding.

Uploaded by

samlithika309
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views7 pages

Java Sorting Algorithms Implementation

The document contains a Java program that implements various sorting algorithms, including selection sort, insertion sort, and their reverse versions for both integers and strings. It allows users to input integers and strings, sorts them using the defined algorithms, and prints the results after each pass. The program demonstrates the sorting process step-by-step for better understanding.

Uploaded by

samlithika309
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Name: Lithika Sambangi

Unit 7 project
[Link]

1.
2. import [Link].*;
3. public class MyProgram
4. {
5. public static void intSelectionSort(ArrayList<Integer> elements)
6. {
7. int count = 0;
8. [Link]("Printing Unsorted ArrayList");
9. for (Integer c: elements)
10. {
11. [Link](c + " ");
12. }
13.
14. [Link]("\nPrinting Each Pass Through the Selection Sort");
15.
16. for (int j = 0; j < [Link]() - 1; j++)
17. {
18. count++;
19. int minIndex = j;
20. for (int a = j + 1; a < [Link](); a++)
21. {
22. if([Link](a) < [Link](minIndex))
23. {
24. minIndex = a;
25. }
26. }
27.
28. Integer temp = [Link](j);
29. [Link](j, [Link](minIndex));
30. [Link](minIndex, temp);
31.
32. [Link]("Pass " + count + ": ");
33. for (Integer b: elements)
34. {
35. [Link](b + " ");
36. }
37. [Link]();
38. }
39. }
40.
41. public static void intInsertionSort(ArrayList<Integer> elements)
42. {
43. int count = 0;
44. [Link]("Printing Unsorted ArrayList");
45. for (Integer c: elements)
46. {
47. [Link](c + " ");
48. }
49.
50. [Link]("\nPrinting Each Pass Through the Insertion Sort");
51.
52. for (int j = 1; j < [Link](); j++)
53. {
54. int temp = [Link](j);
55. int possibleIndex = j;
56. count++;
57.
58. while (possibleIndex > 0 && temp < [Link](possibleIndex - 1))
59. {
60. [Link](possibleIndex, [Link](possibleIndex - 1));
61. possibleIndex--;
62. }
63. [Link](possibleIndex, temp);
64. [Link]("Pass " + count + ": ");
65. for (int b: elements)
66. {
67. [Link](b + " ");
68. }
69. [Link]();
70.
71.
72. }
73. [Link]();
74. }
75.
76. public static void intSelectionSortR(ArrayList<Integer> elements)
77. {
78. int count = 0;
79. [Link]("Printing Unsorted ArrayList");
80. for (Integer c: elements)
81. {
82. [Link](c + " ");
83. }
84.
85. [Link]("\nPrinting Each Pass Through the Reverse Selection Sort");
86.
87. for (int j = 0; j < [Link]() - 1; j++)
88. {
89. count++;
90. int minIndex = j;
91. for (int a = j + 1; a < [Link](); a++)
92. {
93. if([Link](a) > [Link](minIndex))
94. {
95. minIndex = a;
96. }
97. }
98.
99. Integer temp = [Link](j);
100. [Link](j, [Link](minIndex));
101. [Link](minIndex, temp);
102.
103. [Link]("Pass " + count + ": ");
104. for (Integer b: elements)
105. {
106. [Link](b + " ");
107. }
108. [Link]();
109. }
110. [Link]();
111. }
112.
113. public static void intInsertionSortR(ArrayList<Integer> elements)
114. {
115. int count = 0;
116. [Link]("Printing Unsorted ArrayList");
117. for (Integer c: elements)
118. {
119. [Link](c + " ");
120. }
121.
122. [Link]("\nPrinting Each Pass Through the Reverse Insertion Sort");
123.
124. for (int j = 1; j < [Link](); j++)
125. {
126. int temp = [Link](j);
127. int possibleIndex = j;
128. count++;
129.
130. while (possibleIndex > 0 && temp > [Link](possibleIndex - 1))
131. {
132. [Link](possibleIndex, [Link](possibleIndex - 1));
133. possibleIndex--;
134. }
135. [Link](possibleIndex, temp);
136. [Link]("Pass " + count + ": ");
137. for (int b: elements)
138. {
139. [Link](b + " ");
140. }
141. [Link]();
142.
143. }
144. [Link]();
145. }
146.
147. public static void stringSelectionSort(ArrayList<String> arr)
148. {
149. int count = 0;
150. [Link]("Printing Unsorted ArrayList");
151. for (String c: arr)
152. {
153. [Link](c + " ");
154. }
155.
156. [Link]("\nPrinting Each Pass Through the Selection Sort");
157.
158. for (int i = 0; i < [Link]() -1 ; i++)
159. {
160. int max = i;
161. count++;
162.
163. for (int j = i + 1; j < [Link]() ; j++)
164. {
165. if ([Link](max).compareTo([Link](j)) > 0)
166. {
167. max = j;
168.
169. }
170. }
171.
172. String switchy = [Link](i);
173. [Link](i, [Link](max));
174. [Link](max, switchy);
175.
176. [Link]("Pass " + count + ": ");
177. for (String b: arr)
178. {
179. [Link](b + " ");
180. }
181. [Link]();
182.
183. }
184. [Link]();
185. }
186.
187. public static void stringInsertionSort(ArrayList<String> arr)
188. {
189. int count = 0;
190. [Link]("Printing Unsorted ArrayList");
191. for (String c: arr)
192. {
193. [Link](c + " ");
194. }
195.
196. [Link]("\nPrinting Each Pass Through the Insertion Sort");
197. for(int i = 1; i < [Link](); i++)
198. {
199. String storage = [Link](i);
200. int index = i - 1;
201. count++;
202.
203. while (index >= 0 && [Link](index).compareTo(storage) > 0)
204. {
205. [Link](index + 1, [Link](index));
206. index--;
207. }
208. [Link](index + 1, storage);
209. [Link]("Pass " + count + ": ");
210. for (String s : arr)
211. {
212. [Link](s + " ");
213. }
214. [Link]("");
215. }
216. [Link]();
217. }
218.
219. public static void stringInsertionSortR(ArrayList<String> arr)
220. {
221. int count = 0;
222. [Link]("Printing Unsorted ArrayList");
223. for (String c: arr)
224. {
225. [Link](c + " ");
226. }
227.
228. [Link]("\nPrinting Each Pass Through the Reverse Insertion Sort");
229. for(int i = 1; i < [Link](); i++)
230. {
231. String storage = [Link](i);
232. int index = i;
233. count++;
234.
235. while (index > 0 && [Link]([Link](index - 1)) > 0)
236. {
237. [Link](index, [Link](index - 1));
238. index--;
239. }
240. [Link](index, storage);
241. [Link]("Pass " + count + ": ");
242. for (String s : arr)
243. {
244. [Link](s + " ");
245. }
246. [Link]("");
247.
248.
249. }
250. }
251.
252.
253. public static void stringSelectionSortR(ArrayList<String> arr)
254. {
255. int count = 0;
256. [Link]("Printing Unsorted ArrayList");
257. for (String c: arr)
258. {
259. [Link](c + " ");
260. }
261.
262. [Link]("\nPrinting Each Pass Through the Reverse Selection Sort");
263.
264. for (int i = [Link]() - 1; i >= 1; i--)
265. {
266. int max = i;
267. count++;
268.
269. for (int j = i; j >= 0; j--)
270. {
271. if ([Link](j).compareTo([Link](max)) < 0)
272. {
273. max = j;
274. }
275. }
276.
277. [Link]("Pass " + count + ": ");
278. for (String b: arr)
279. {
280. [Link](b + " ");
281. }
282. [Link]();
283. String switchy = [Link](i);
284. [Link](i, [Link](max));
285. [Link](max, switchy);
286.
287. }
288. }
289.
290. public static void main(String[] args)
291. {
292. Scanner scan = new Scanner([Link]);
293.
294. //Step 1
295. [Link]("Please enter 5 integers");
296. ArrayList<Integer> list1 = new ArrayList<Integer>();
297. for (int i = 0; i < 5; i++)
298. {
299. [Link]([Link]());
300. }
301.
302. intSelectionSort(list1);
303. intSelectionSortR(list1);
304.
305.
306. [Link]("\nPlease enter another 5 integers");
307. ArrayList<Integer> list2 = new ArrayList<Integer>();
308. for (int i = 0; i < 5; i++)
309. {
310. [Link]([Link]());
311. }
312.
313. intInsertionSort(list2);
314. intInsertionSortR(list2);
315.
316.
317. ArrayList<String> list3 = new ArrayList<String>();
318. /* [Link]("banana");
319. [Link]("apple");
320. [Link]("carrot");
321. [Link]("hello");
322. [Link]("bannana");
323. [Link]("math");*/
324. [Link]("d");
325. [Link]("c");
326. [Link]("e");
327. [Link]("b");
328. [Link]("a");
329. stringSelectionSort(list3);
330. stringSelectionSortR(list3);
331.
332.
333.
334. ArrayList<String> list4 = new ArrayList<String>();
335. /* [Link]("orange");
336. [Link]("algebra");
337. [Link]("stanford");
338. [Link]("physics");
339. [Link]("lithika");
340. [Link]("dog");*/
341. [Link]("e");
342. [Link]("a");
343. [Link]("c");
344. [Link]("b");
345. [Link]("d");
346. stringInsertionSort(list4);
347. stringInsertionSortR(list4);
348. }
349.
350. }

Common questions

Powered by AI

The reverse sorting method implementations for both integer and string sorting involve modifying the comparison operators to achieve descending order sorting. This alteration has minimal impact on algorithm complexity as the number of operations remains similar to regular sorting. However, reverse sorting may not reveal the same best-case performance improvements seen in insertion sort with nearly sorted arrays since elements increasingly require relocation to their farther positions in absolute reverse order compared to forward sorting .

In the `stringSelectionSort` method, during each pass through the array, the program selects the smallest string based on lexicographical order, starting from the current index to the end. This is done by comparing strings using the `compareTo` method. Once the smallest string is found, it is swapped with the string at the current index, which is part of the unsorted portion of the array. The method continues this process until the entire array is sorted, printing the state of the array after each pass .

The sorting algorithms implemented in the Java program—selection sort and insertion sort—have a computational complexity of O(n^2) in the average and worst cases. This is because both algorithms use nested loops to compare and shift elements. Selection sort involves finding the minimum element in the unsorted portion for each pass, while insertion sort involves shifting elements to insert the new element in the sorted portion. Although they have similar time complexities, insertion sort may perform better on partially sorted arrays as it makes fewer shifts when elements are already in order compared to the direct swaps in selection sort .

The `compareTo` method in the `stringInsertionSort` algorithm is used to maintain the correct lexicographical order among string elements. It compares two strings and determines their order by returning a negative, zero, or positive integer. This result dictates whether one string precedes or follows another. During sorting, if the current string is less than the preceding string (returns a positive integer), it shifts the preceding strings forward to insert the current string at its correct position. This ensures the sorted portion remains in order and correctly places each new string into its position .

The `intSelectionSortR` method differs from `intSelectionSort` by changing the comparison operation to sort in reverse order. While `intSelectionSort` uses the condition `elements.get(a) < elements.get(minIndex)` to find the smallest element, `intSelectionSortR` uses `elements.get(a) > elements.get(minIndex)`, which selects the largest element instead, resulting in a descending sort .

The `main` method demonstrates the usage of both integer and string sorting methods by prompting the user to enter integers and subsequently passing these to both `intSelectionSort` and `intInsertionSort` methods for sorting. It also constructs string lists by directly adding string elements to them, then applies `stringSelectionSort` and `stringInsertionSort` methods to demonstrate sorting. Each sort method is called along with its reverse variant, effectively illustrating sorting functionality for both data types .

Encapsulating sorting functionalities into static sorting methods benefits the program design by promoting modularity and reusability. These methods can be called independently of any specific instance, making them versatile for multiple applications or lists. This separation also improves code readability and maintainability by isolating specific tasks, allowing developers to update sorting logic without impacting other parts of the code. It allows for straightforward integration into other class methods or programs needing sorting capabilities .

The use of `System.out.print` statements in sorting methods aids in understanding the sorting process by providing a visual representation of the array or list after each pass or iteration. This allows a clearer insight into how each element shifts and affects overall order. By printing the intermediate states, users can trace the progress of the sort algorithm, analyze how individual elements are manipulated, and better comprehend the functioning and intermediate steps of the algorithms .

The `intSelectionSort` method implements the selection sort algorithm for a list of integers. It involves iterating through the list multiple times, each time selecting the smallest (or least in terms of value) element from the unsorted portion and swapping it with the first unsorted element. This process is repeated until the entire array is sorted. The method also prints the list after each pass through the sorting process .

The `intInsertionSort` method differs from `intSelectionSort` in its sorting logic. Instead of repeatedly selecting the minimum element, the insertion sort builds up a sorted portion of the array one element at a time by comparing each element to those in the sorted portion and inserting it in its correct position. The `intInsertionSort` method shifts elements of the sorted portion to make space for the inserted element, whereas `intSelectionSort` swaps elements directly .

You might also like