Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(java) Hello! I am trying to write a program that among other things sorts an ArrayList using quick sort, I have tried many different things

(java)

Hello!

I am trying to write a program that among other things sorts an ArrayList using quick sort, I have tried many different things but right now I am getting a null pointer exception. Any suggestions would be appreciated.

Thank you, here is the code

import java.util.*;

public class BuiltIn {

private static final int[] ArrayS = null;

public static void main(String[] args) { Random r = new Random(); ArrayList Array = new ArrayList<>(10); System.out.println("Array:");

for (int i = 0; i < 10; i++) { int ran = r.nextInt(1000); Array.add(ran); System.out.println(Array.get(i)); }

int right; int left; for (int i = 0; i < 10; i++) { ArrayS = new int[Array.size()]; ArrayS[i] = (int) Array.get(i); System.out.println(ArrayS[i]);

}

ArrayS = sort(ArrayS, Array, left, right); System.out.println("Sorted Array:"); for (int i = 0; i < 10; i++) { System.out.println(ArrayS[i]); }

Stack Stack = new Stack<>(); Queue Queue = new LinkedList<>();

System.out.println("Queue:"); for (int i = 0; i < 10; i++) { Queue.add(Array.get(i)); System.out.println(Queue.remove());

}

System.out.println("Stack:");

for (int i = 0; i < 10; i++) { Stack.add(Array.get(i));

}

for (int i = 0; i < 10; i++) { System.out.println(Stack.pop()); }

}

private static int sort(int ArrayS[], ArrayList Array, int left, int right) { int index = partition(ArrayS, Array, left, right); if (left < index - 1) { sort(ArrayS, Array, left, index+1); } if (index < right) { sort(ArrayS, Array, index-1, right); } return index;

}

private static int partition(int ArrayS[], ArrayList Array, int left, int right) { right=ArrayS[0]; left=ArrayS[9]; int i = left; int j = right; int pivot = ArrayS[(left + right) / 2]; while (i <= j) { while (ArrayS[i] < pivot) { i++; } while (ArrayS[j] > pivot) { j--; } if (i <= j) { int temp = ArrayS[i]; ArrayS[i] = ArrayS[j]; ArrayS[j] = temp; i++; j--; } } for (int l = 0;l<10;l++){ System.out.println(ArrayS[l]); } return i; }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database 11g SQL

Authors: Jason Price

1st Edition

0071498508, 978-0071498500

More Books

Students also viewed these Databases questions

Question

What are Decision Trees?

Answered: 1 week ago

Question

What is meant by the Term Glass Ceiling?

Answered: 1 week ago