USE GNU PROLOG! Reimplement the Quicksort.  In the given example, the first (left-most) element of the given list is selected as the pivot. In this question,  you must choose the second element of the list as the pivot. Hint: You can represent the input  list into pairs: [First | [Pivot | Tail]]. You must write comments to indicate the size-n problem, stopping condition and its return value, size m-problems, and construction of the size-n  problem from size-m problems.  Test case: | ?- qsort2([8, 3, 4, 12, 25, 4, 6, 1, 9, 22, 6], Sorted). It returns: Sorted = [1,3,4,4,6,6,8,9,12,22,25]

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

USE GNU PROLOG!


Reimplement the Quicksort
.  In the given example, the first (left-most) element of the given list is selected as the pivot. In this question,  you must choose the second element of the list as the pivot.

Hint: You can represent the input  list into pairs: [First | [Pivot | Tail]]. You must write comments to indicate the size-n problem, stopping condition and its return value, size m-problems, and construction of the size-n  problem from size-m problems. 

Test case:
| ?- qsort2([8, 3, 4, 12, 25, 4, 6, 1, 9, 22, 6], Sorted). It returns:
Sorted = [1,3,4,4,6,6,8,9,12,22,25]  

qsort(,) :-!.
% empty list is already sorted
qsort([Pivot Tail], Sorted):- % Take first number as pivot
split(Pivot, Tail, L1, L2),
Quick Sort Code in Prolog
% sort first part
% sort second part
append(Sorted 1, [Pivot|Sorted2], Sorted).
split(,0,0,0).
qsort(L1, Sorted1),
qsort(L2, Sorted2),
CSE240
11/19/2002
split(Pivot,[X|T],[X|Le],Gt):-
split(Pivot,
X=<Pivot, split(Pivot, T,Le, Gt).
[X|T],Le,[X|Gt]):-
X > Pivot, split(Pivot, T,Le, Gt).
|| 2- [quicksort).
compiling /afs/asu.edu/users/y
/afs/asu.edu/users/y/c/h/ychen
980 bytes written, 7 ms
yes
?- qsort ([3, 2,5, 3, 1], S).
S = [1,2,3,3,5) ?
% stopping condition
% take first from Tail
% and put it into Le
% take first from Tail
% and put it into Gt
Ch 5
4
Transcribed Image Text:qsort(,) :-!. % empty list is already sorted qsort([Pivot Tail], Sorted):- % Take first number as pivot split(Pivot, Tail, L1, L2), Quick Sort Code in Prolog % sort first part % sort second part append(Sorted 1, [Pivot|Sorted2], Sorted). split(,0,0,0). qsort(L1, Sorted1), qsort(L2, Sorted2), CSE240 11/19/2002 split(Pivot,[X|T],[X|Le],Gt):- split(Pivot, X=<Pivot, split(Pivot, T,Le, Gt). [X|T],Le,[X|Gt]):- X > Pivot, split(Pivot, T,Le, Gt). || 2- [quicksort). compiling /afs/asu.edu/users/y /afs/asu.edu/users/y/c/h/ychen 980 bytes written, 7 ms yes ?- qsort ([3, 2,5, 3, 1], S). S = [1,2,3,3,5) ? % stopping condition % take first from Tail % and put it into Le % take first from Tail % and put it into Gt Ch 5 4
Expert Solution
Step 1: Here's the implementation of Quicksort with the second element of the list as the pivot

Here's the implementation of Quicksort with the second element of the list as the pivot:

CODE:

% Quicksort with second element as pivot
qsort2([], []).   % base case: empty list, return empty list
qsort2([X], [X]). % base case: single element list, return the same list

qsort2([First, Pivot | Tail], Sorted) :-
  % Divide the list into two sublists based on the pivot
  partition(Pivot, [First|Tail], Left, Right),

  % Recursively sort the two sublists
  qsort2(Left, SortedLeft),
  qsort2(Right, SortedRight),

  % Combine the sorted sublists and the pivot
  append(SortedLeft, [Pivot|SortedRight], Sorted).

% Helper predicate to partition a list into two sublists
% based on a pivot element
partition(_, [], [], []).
partition(Pivot, [X|Xs], [X|Left], Right) :-
  X =< Pivot,
  partition(Pivot, Xs, Left, Right).
partition(Pivot, [X|Xs], Left, [X|Right]) :-
  X > Pivot,
  partition(Pivot, Xs, Left, Right).

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Time complexity
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education