Notes
![]() ![]() Notes - notes.io |
public class QuickSort {
public static void main(String[] args) {
// Create an array to store numbers
int[] numbers = new int[10];
// Create a random number generator
Random randomGenerator = new Random();
// Fill the array with unique random numbers from 0 to 100
int filledCount = 0;
while (filledCount < 10) {
int randomNumber = randomGenerator.nextInt(101); // Random number between 0 and 100
// Check if the number is already in the array
boolean isNewNumber = true;
for (int i = 0; i < filledCount; i++) {
if (numbers[i] == randomNumber) {
isNewNumber = false; // If the number is already in the array, try again
break;
}
}
// If the number is new, add it to the array
if (isNewNumber) {
numbers[filledCount] = randomNumber;
filledCount++;
}
}
// Prints the random array generated
System.out.println("Numbers to be Sorted:");
for (int i = 0; i < numbers.length; i++) {
System.out.print(numbers[i] + " ");
}
System.out.println(); // For formatting
// QuickSort logic without separate methods
int low = 0;
int high = numbers.length - 1;
// Stack to simulate recursion
int[] stack = new int[numbers.length];
int top = -1;
// Push initial low and high to the stack
stack[++top] = low;
stack[++top] = high;
// Iterative QuickSort using stack
while (top >= 0) {
// Pop high and low
high = stack[top--];
low = stack[top--];
if (low < high) {
// Select a random pivot index
int pivotIndex = randomGenerator.nextInt(high - low + 1) + low;
int pivot = numbers[pivotIndex];
// Show the pivot before partitioning
System.out.println("Pivot: " + pivot);
// Swap the pivot with the last element
int temp = numbers[pivotIndex];
numbers[pivotIndex] = numbers[high];
numbers[high] = temp;
int i = low - 1;
// Partition the array and get the pivot index
for (int j = low; j < high; j++) {
if (numbers[j] < pivot) {
i++;
temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
}
// Place the pivot in its correct position
temp = numbers[i + 1];
numbers[i + 1] = numbers[high];
numbers[high] = temp;
// Pivot index after partitioning
int newPivotIndex = i + 1;
// Print the array after partitioning
System.out.println("Array after partitioning (pivot " + numbers[newPivotIndex] + "):");
for (int i1 = 0; i1 < numbers.length; i1++) {
System.out.print(numbers[i1] + " ");
}
System.out.println(); // For formatting
// Push the left and right subarrays to the stack
stack[++top] = low;
stack[++top] = newPivotIndex - 1;
stack[++top] = newPivotIndex + 1;
stack[++top] = high;
}
}
// Print the array after sorting
System.out.println("After sorting:");
for (int i = 0; i < numbers.length; i++) {
System.out.print(numbers[i] + " ");
}
}
}
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team