Java Has Built In Data Structures!? Objective: Yes, the data structures you may
ID: 3668987 • Letter: J
Question
Java Has Built In Data Structures!?
Objective: Yes, the data structures you may have agonized over is actually already implemented in java, but now you know the details behind each of them. Write a program that does the following: Create and populate an ArrayList of integers with 10 to 20 numbers where each number could be between 0-99. Then print that out. Sort the ArrayList in ascending order either by writing QuickSort or MergeSort. You may NOT use the collections sort. Write it yourself. Then print that out. Populate a Queue with the ArrayList, and then dequeue each element and print it out until the queue is empty. Populate a Stack with the ArrayList, and then pop each element and print it out until the stack is empty. Do this process 3 times.
Explanation / Answer
DataStructures.java
package assign3;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;
import java.util.Stack;
public class DataStructures {
public static void main(String[] args) {
ArrayList<Integer> numList = new ArrayList<Integer>();
Random rand = new Random();
// As mentioned in question currently taking 10 numbers in to the list
for (int i = 0; i < 10; i++) {
// getting random numbers in between 0 to 99
numList.add(rand.nextInt(99 - 0) + 0);
}
// Displaying the elements in the array list
System.out.println("Elements in the ArrayList:");
for (int num : numList) {
System.out.print(num + " ");
}
// Quick Sort to arrange the list in ascending order
int low = 0;
int high = numList.size() - 1;
quickSort(numList, low, high);
System.out.println(" After Sorting using quick sort:");
for (int num : numList) {
System.out.print(num + " ");
}
System.out.println(" Iterating for three times:");
// Iterating for three times
for (int i = 0; i < 3; i++) {
// Populate a Queue with the ArrayList
Queue<Integer> myQueue = new LinkedList<Integer>();
for (int num : numList) {
myQueue.add(num);
}
System.out.println(" dequeued(removed) element from the Queue:");
while (myQueue.size() != 0) {
System.out.print(myQueue.remove() + " ");
}
// Populate a Stack with the ArrayList
System.out.println();
Stack<Integer> myStack = new Stack<Integer>();
for (int num : numList) {
myStack.push(num);
}
System.out.println(" poped element from the stack");
int size = myStack.size();
for (int j = 0; j < size; j++) {
System.out.print(myStack.pop() + " ");
}
System.out.println(" ");
}
}
/**
* Method to sort the array list in ascending order quick sort
*
* @param arr
* @param low
* @param high
*/
public static void quickSort(ArrayList<Integer> arr, int low, int high) {
if (arr == null || arr.size() == 0)
return;
if (low >= high)
return;
// pick the pivot
int middle = low + (high - low) / 2;
int pivot = arr.get(middle);
// make left < pivot and right > pivot
int i = low, j = high;
while (i <= j) {
while (arr.get(i) < pivot) {
i++;
}
while (arr.get(j) > pivot) {
j--;
}
if (i <= j) {
int temp = arr.get(i);
arr.set(i, arr.get(j));
arr.set(j, temp);
i++;
j--;
}
}
// recursively sort two sub parts
if (low < j)
quickSort(arr, low, j);
if (high > i)
quickSort(arr, i, high);
}
}
Output:
Elements in the ArrayList:
71 95 68 21 76 47 55 54 31 16
After Sorting using quick sort:
16 21 31 47 54 55 68 71 76 95
Iterating for three times:
dequeued(removed) element from the Queue:
16 21 31 47 54 55 68 71 76 95
poped element from the stack
95 76 71 68 55 54 47 31 21 16
dequeued(removed) element from the Queue:
16 21 31 47 54 55 68 71 76 95
poped element from the stack
95 76 71 68 55 54 47 31 21 16
dequeued(removed) element from the Queue:
16 21 31 47 54 55 68 71 76 95
poped element from the stack
95 76 71 68 55 54 47 31 21 16
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.