Question

Sorting Sort the following array using the quick sort algorithm: (4 Marks) a. 12 26 8 9 7 0 4 Pivot selection is defined to b

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Ques1

package sort;

/*pseudo code
* sort( ar[], low, high):
*    if low<hight{
*    int pi=partition(ar,low,high)
*        sort(ar,low,pi)
*        sort(ar,pi,high)
*        }
* partition(ar[],low,high){
*    int p=ar[low]
*    int j=high
*    while i<-high+1 greater then l{
*    if ar[j] less then p{
*            i--
*            swap ar[i],ar[j]
*            j--
*        }
* swap ar[i-1],ar[l]
* return i-1
*    }
* }
*/
public class QuickSort
{
static int k=1;
public static int part(int ar[], int l, int h)
{
int p = ar[l]; //to first element for array as pivote   
int j=h; //j as highest
int i = (h+1); //after highest index
while (j>l) //run loop j greater the low
{
  
if (ar[j]>= p) { //if jth element greater then p
  
i--; //swap ith and jth element
int t = ar[i];
ar[i] = ar[j];
ar[j] = t;
}
j--; //go to lower element
}   
  
int t = ar[i-1]; //swap pivot if it is not at right place
ar[i-1] = ar[l];
ar[l] = t;
return i-1;
}
  
public static void sort(int ar[], int l, int h)
{
if (l < h)
{
   //using divide and concor
int pi = part(ar, l, h); //find pivote index
System.out.println("Swap :"+(k++)+" Pivot: "+ar[pi]);
for (int i=0; i<ar.length; ++i)
System.out.print(ar[i]+" ");
System.out.println();
sort(ar, l, pi-1); //sort array before pivot
sort(ar, pi+1, h); //after pivot
}
}
  
// Driver program
public static void main(String args[])
{
int ar[] = {4,0,12,2,6,1,8,5,9,7};
System.out.println("Initial array "+"\n Pivot: "+ar[0]);
for (int i=0; i<ar.length; ++i)
System.out.print(ar[i]+" ");
System.out.println();
sort(ar, 0, ar.length-1);
System.out.println("Sorted Array:");
for (int i=0; i<ar.length; ++i)
System.out.print(ar[i]+" ");
  
}
}

output

Initial array
Pivot: 4
4 0 12 2 6 1 8 5 9 7
Swap :1 Pivot: 4
2 0 1 4 12 6 8 5 9 7
Swap :2 Pivot: 2
1 0 2 4 12 6 8 5 9 7
Swap :3 Pivot: 1
0 1 2 4 12 6 8 5 9 7
Swap :4 Pivot: 12
0 1 2 4 7 6 8 5 9 12
Swap :5 Pivot: 7
0 1 2 4 5 6 7 8 9 12
Swap :6 Pivot: 5
0 1 2 4 5 6 7 8 9 12
Swap :7 Pivot: 8
0 1 2 4 5 6 7 8 9 12
Sorted Array:
0 1 2 4 5 6 7 8 9 12

Ques2

package sort;
/*Time complexity =O(N)
* as it is comparing each element(0,n) to piovt and swapping
* Space complexity=O(1) no extra space needed to store array element
*
*/

public class SeperateNegativeElement {

   public static void arrangeNegAndPos(int ar[]) {
  
   int pivot=0; //initiate pivot as 0th index
   for (int i = 0; i < ar.length; i++) { //from 0 to n
  
       if (ar[i] < 0) { //if ith element -ve
   if (i != pivot) { //and ith index is not pivot
   int temp = ar[i]; //swap them
   ar[i] = ar[pivot];
   ar[pivot] = temp;
   }
   pivot++; //now increase the pivot
   }
   }
   }
   public static void main(String[] args) {
       int[] ar = {-5,10,3,-2,1,0};
       arrangeNegAndPos(ar);
       for(int i:ar){
  
   System.out.print(i);
   System.out.print(", ");
   }

   }

}

output

-5, -2, 3, 10, 1, 0,

Ques 3.

public class SelectionSort {
   public static void selSort(int[] arr){

   for (int i=0;i<arr.length;i++)
   {
       // int smallerNumber = arr[i]; //make current element as smallest
       int index=getMin(arr,i,arr.length-1);
      
  
         
     
   if(index!=-1){ //if smallest found
   int smallerNumber=arr[i]; //swap it
   arr[i]=arr[index];
   arr[index] = smallerNumber;
   }
   }
     
  
   }

   public static int getMin(int arr[],int startIndex,int endIndex){
       int index=startIndex;
       for(int j=startIndex+1;j<=endIndex;j++){ //find smallest element in ar[i.....n]
      
    if(arr[index]<arr[j]){   
       
        index=j; //remember i of smallest
    }
       }
    return index;
   }
   public static void main(String[] args) {
     
int[] arrayOne = {10,34,2,56,12,64,18,14,43,52,69,
7,67,88,42,27,45,6,19,39,34,51,70};
selSort(arrayOne);
for(int i:arrayOne){
  
System.out.print(i);
System.out.print(", ");
}

   }

}

output

88, 70, 69, 67, 64, 56, 52, 51, 45, 43, 42, 39, 34, 34, 27, 19, 18, 14, 12, 10, 7, 6, 2,

Add a comment
Answer #2

// Java implementation of QuickSort

import java.io.*;


class GFG{

// A utility function to swap two elements

static void swap(int[] arr, int i, int j)

{

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

}


/* This function takes last element as pivot, places

the pivot element at its correct position in sorted

array, and places all smaller (smaller than pivot)

to left of pivot and all greater elements to right

of pivot */

static int partition(int[] arr, int low, int high)

{

// pivot

int pivot = arr[high];

// Index of smaller element and

// indicates the right position

// of pivot found so far

int i = (low - 1);


for(int j = low; j <= high - 1; j++)

{

// If current element is smaller

// than the pivot

if (arr[j] < pivot)

{

// Increment index of

// smaller element

i++;

swap(arr, i, j);

}

}

swap(arr, i + 1, high);

return (i + 1);

}


/* The main function that implements QuickSort

arr[] --> Array to be sorted,

low --> Starting index,

high --> Ending index

*/

static void quickSort(int[] arr, int low, int high)

{

if (low < high)

{

// pi is partitioning index, arr[p]

// is now at right place

int pi = partition(arr, low, high);


// Separately sort elements before

// partition and after partition

quickSort(arr, low, pi - 1);

quickSort(arr, pi + 1, high);

}

}


// Function to print an array

static void printArray(int[] arr, int size)

{

for(int i = 0; i < size; i++)

System.out.print(arr[i] + " ");

System.out.println();

}


// Driver Code

public static void main(String[] args)

{

int[] arr = { 10, 7, 8, 9, 1, 5 };

int n = arr.length;

quickSort(arr, 0, n - 1);

System.out.println("Sorted array: ");

printArray(arr, n);

}

}




answered by: Shivani Sharma
Add a comment
Know the answer?
Add Answer to:
Sorting Sort the following array using the quick sort algorithm: (4 Marks) a. 12 26 8...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Sorting algorithm: quick sort Exercise One (20 marks) Given the following program body for implementing Quick...

    Sorting algorithm: quick sort Exercise One (20 marks) Given the following program body for implementing Quick sort, complete the program by writing code where required import java.util.Random; public class QuickSort public void quickSectlinti] A) QuickSort/A, O, A.length-1); private void guickSortlin Aiat low.int high) //Complete the code for the quicksort method (5 marks] private void swaplint[] a, int indexl, int index2) //Complete the code for the swap method [3 marks] private int setPivotlint low, int high) Random rand = new Random();...

  • 6) Assume that we are using quick sort algorithm to sort the following elements in the...

    6) Assume that we are using quick sort algorithm to sort the following elements in the array 26, 15,30,11,8,17 22, 40, 4, 10. Use the first element in the array as pivot. (20 pts.) 1- How total iterations it would take to complete the sorting process? 2- Simulate the entire sorting process. (If you need additional space, complete it at the other side of the paper) public static void quick_sort(intl] a, int left, int right) if (left < right) (...

  • Assume that you are sorting an array of 8 elements with quick sort. You just finished...

    Assume that you are sorting an array of 8 elements with quick sort. You just finished the first pass and the array looks like below. Which statement is true for the pivot value? 4 8 12 16 18 20 22 24 QUICKSORT ALGORITHM Quicksort selects a specific value called a pivot and rearranges the array into two parts (called partioning). If the array is randomly ordered, it does not matter which element is the pivot. For simplicity, the first element...

  • 1. Which is the best sorting algorithm for larger arrays if all the items can not...

    1. Which is the best sorting algorithm for larger arrays if all the items can not fit in main memory? selection sort insertion sort quicksort Merge sort 2. Which sorting algorithm sorts items without comparing them? quick sort radix sort merge sort Insertion sort 3 What is the average running time for quicksort? O(n2) O(logn) O(nlogn) O(n) O(n2logn) 4. Examine the steps of the following algorithm and write the name of the algorithm described in the blank provided: Recursively divide...

  • . Shell sort is a sorting algorithm similar to insertion sort. Research shell sort and apply...

    . Shell sort is a sorting algorithm similar to insertion sort. Research shell sort and apply that to the following array. Show your work in Detail. [15 points] 45 20 50 10 80 30 60 70 40 90 2. Is Shell sort a stable sorting algorithm? Answer this with an example. [10 points] 3. Apply Merge Sort to sort the following list. Show your work in Detail. [15 Points] 45 20 50 10 80 30 60 70 40 90 4....

  • Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. E...

    Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...

  • Using C++, sort an array of 10,000 elements using the quick sort algorithm as follows: a....

    Using C++, sort an array of 10,000 elements using the quick sort algorithm as follows: a. Sort the array using pivot as the middle element of the array. b. Sort the array using pivot as the median of the first, last, and middle elements of the array. c. Sort the array using pivot as the middle element of the array. However, when the size of any sublist reduces to less than 20, sort thesublis t using an insertion sort. d....

  • Perform the partition method of quick sort once on the array [8, 12, 2, 15, 7]....

    Perform the partition method of quick sort once on the array [8, 12, 2, 15, 7]. Show the array after each iteration of the while loop in the partition method. Use the first element (here it is 8) as the pivot. Show the two-sub array after one call to quick sort.

  • Sort this array using QUICK Sort: For each pass... label the pivot. show the array and...

    Sort this array using QUICK Sort: For each pass... label the pivot. show the array and correct values. list recursive calls for each pass.

  • The quick sort algorithm uses a ________ to divide the array into two pieces. Group of...

    The quick sort algorithm uses a ________ to divide the array into two pieces. Group of answer choices divider pivot mid-point key Which of the following statement(s) are true about quick sort? Group of answer choices It does not require additional memory that merge sort does. All of them In practice, it can be faster than merge sort. It can degrade into an O(n2) sort if the pivot-selection scheme is bad. Which sort does not use comparisons? Group of answer...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT