SlideShare ist ein Scribd-Unternehmen logo
1 von 8
Abhishek Pachisia
B.Tech(IT)-II yr
09SETIT144
It is one of the fastest sorting techniques available.
Like binary search, this method uses a recursive, divide
and conquer strategy
The basic idea is to separate a list of items into two
parts, surrounding a distinguished item called the pivot.
At the end of the process, one part will contain items
smaller than the pivot and the other part will contain items
larger than the pivot.
The performance of the whole Quicksort algorithm
depends on how well the pivot is taken.
Let the unsorted list (let it be a)be…

      14     3     2     11     5      8      0       2    9       4      20
                              Unsorted List


  1. Let us take the middle element as pivot.
  2. Then we arrange the array such that all elements greater then 8 is on right
     side and others on left side.(This is the first subdivision producing two sub
     list).
  3. Now, each sub list is subdivided in exactly the same manner. This process
     continues until all sub lists are in order. The list is then sorted. This is a
     recursive process.


       0     2      2     3      4     5          8   9    11      14      20

                                     Sorted List
We choose the value as pivot.(Preferably middle element).
As in binary search, the index of this value is found by (first+last)/2
  where first and last are the indices of the initial and final items in the list.
  We then identify a left_arrow and right_arrow on the far left and far
  right, respectively.(left_arrow and right_arrow initially represent the lowest
  and highest indices of the vector items).
  It looks like:
                                        Pivot


             14    3    2    11     5      8    0     2    9      4      20



          left_arrow                                                  right_arrow
Starting on the right, the right_arrow is moved left until a value less than or
 equal to the pivot is encountered.
 In a similar manner, left_arrow is moved right until a value greater than or
 equal to the pivot is encountered. This is the situation just encountered. Now the
 contents of the two vector items are swapped to produce
                                      Pivot


          14     3       2   11   5      8      0       2    9        4        20


        left_arrow                                               right_arrow

We continue by moving right_arrow left and moving left_arrow right to produce.
                                              Pivot


            4        3   2    2   5       0         8   11   9        14        20


                                             right_arrow
                                      left_arrow
Since right_arrow < left_arrow is TRUE, the first subdivision is complete. At
this stage, numbers smaller than pivot are on the left side and numbers larger
than pivot are on the right side. This produces two sub lists ,they are:

                                               Pivot


    4     3       2      2      5   0           8           11     9      14      20


              Smaller Numbers                                    Larger Numbers

Each sub list can now be sorted by the same function using recursive call to
the sorting function.
List obtained after sorting is :

              0   2      2      3   4     5         8   9   11      14      20

                                        Sorted List
void QuickSort(apvector<int> &list, int left, int right)
{
    int pivot, leftArrow, rightArrow, n;
    leftArrow = left;
    rightArrow = right;
    n=left - right + 1;
    pivot = list[(left + right) / 2];
    do
    {                                                                     Total efficiency
              while (list[rightArrow] > pivot)
                            --rightArrow;
              while (list[leftArrow] < pivot)
                            ++leftArrow;
              if (leftArrow <= rightArrow)
              {
                            Swap_Data(list[leftArrow], list[rightArrow]);
                            ++leftArrow;
                            --rightArrow;
                            level++;
                            printf(“nLevel %d : ”,level);
                            efficiency(n);
              }
    } while (rightArrow >= leftArrow);
    if (left < rightArrow)
               QuickSort(list, left, rightArrow);
    if (leftArrow < right)
               QuickSort(list, leftArrow, right);
}
Since each element ultimately ends up in the correct position, the algorithm
correctly sorts. But how long does it take?.On this basis it is divided into
following three cases.

1. Best Case
2. Worst Case
3. Average Case

Best Case for Quick Sort
The best case for divide-and-conquer algorithms comes when we split the input as evenly as
possible. Thus in the best case, each sub problem is of size n/2. The partition step on each
sub problem is linear in its size. the total efficiency(time taken) in the best case is O(nlog2n).
Worst Case for Quicksort
Suppose instead our pivot element splits the array as unequally as possible. Thus instead of
n/2 elements in the smaller half, we get zero, meaning that the pivot element is the biggest
or smallest element in the array.
 The Average Case for Quicksort
Suppose we pick the pivot element at random in an array of n keys Half the time, the pivot
element will be from the centre half of the sorted array. Whenever the pivot element is from
positions n/4 to 3n/4, the larger remaining sub-array contains at most 3n/4 elements.

Weitere ähnliche Inhalte

Mehr von Abhishek Pachisia (18)

Work breakdown structure
Work breakdown structureWork breakdown structure
Work breakdown structure
 
IEEE 802.11
IEEE 802.11IEEE 802.11
IEEE 802.11
 
Cloud Service Models
Cloud Service ModelsCloud Service Models
Cloud Service Models
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
 
Perl
PerlPerl
Perl
 
Rpc
RpcRpc
Rpc
 
Program Evaluation and Review Technique (PERT)
Program Evaluation and Review Technique (PERT)Program Evaluation and Review Technique (PERT)
Program Evaluation and Review Technique (PERT)
 
Hydrogen energy
Hydrogen energyHydrogen energy
Hydrogen energy
 
Rms titanic
Rms titanicRms titanic
Rms titanic
 
Vb.net (loop structure)
Vb.net (loop structure)Vb.net (loop structure)
Vb.net (loop structure)
 
Matrix Representation Of Graph
Matrix Representation Of GraphMatrix Representation Of Graph
Matrix Representation Of Graph
 
Tree
TreeTree
Tree
 
File System Implementation
File System ImplementationFile System Implementation
File System Implementation
 
Exception handling
Exception handlingException handling
Exception handling
 
Condolence letter
Condolence letterCondolence letter
Condolence letter
 
TECHNOLOGY IN COMMUNICATION
TECHNOLOGY  IN  COMMUNICATION TECHNOLOGY  IN  COMMUNICATION
TECHNOLOGY IN COMMUNICATION
 
Consumer protection act,1986
Consumer protection act,1986Consumer protection act,1986
Consumer protection act,1986
 
Motivatoin
MotivatoinMotivatoin
Motivatoin
 

Kürzlich hochgeladen

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 

Kürzlich hochgeladen (20)

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 

Quick sort

  • 2. It is one of the fastest sorting techniques available. Like binary search, this method uses a recursive, divide and conquer strategy The basic idea is to separate a list of items into two parts, surrounding a distinguished item called the pivot. At the end of the process, one part will contain items smaller than the pivot and the other part will contain items larger than the pivot. The performance of the whole Quicksort algorithm depends on how well the pivot is taken.
  • 3. Let the unsorted list (let it be a)be… 14 3 2 11 5 8 0 2 9 4 20 Unsorted List 1. Let us take the middle element as pivot. 2. Then we arrange the array such that all elements greater then 8 is on right side and others on left side.(This is the first subdivision producing two sub list). 3. Now, each sub list is subdivided in exactly the same manner. This process continues until all sub lists are in order. The list is then sorted. This is a recursive process. 0 2 2 3 4 5 8 9 11 14 20 Sorted List
  • 4. We choose the value as pivot.(Preferably middle element). As in binary search, the index of this value is found by (first+last)/2 where first and last are the indices of the initial and final items in the list. We then identify a left_arrow and right_arrow on the far left and far right, respectively.(left_arrow and right_arrow initially represent the lowest and highest indices of the vector items). It looks like: Pivot 14 3 2 11 5 8 0 2 9 4 20 left_arrow right_arrow
  • 5. Starting on the right, the right_arrow is moved left until a value less than or equal to the pivot is encountered. In a similar manner, left_arrow is moved right until a value greater than or equal to the pivot is encountered. This is the situation just encountered. Now the contents of the two vector items are swapped to produce Pivot 14 3 2 11 5 8 0 2 9 4 20 left_arrow right_arrow We continue by moving right_arrow left and moving left_arrow right to produce. Pivot 4 3 2 2 5 0 8 11 9 14 20 right_arrow left_arrow
  • 6. Since right_arrow < left_arrow is TRUE, the first subdivision is complete. At this stage, numbers smaller than pivot are on the left side and numbers larger than pivot are on the right side. This produces two sub lists ,they are: Pivot 4 3 2 2 5 0 8 11 9 14 20 Smaller Numbers Larger Numbers Each sub list can now be sorted by the same function using recursive call to the sorting function. List obtained after sorting is : 0 2 2 3 4 5 8 9 11 14 20 Sorted List
  • 7. void QuickSort(apvector<int> &list, int left, int right) { int pivot, leftArrow, rightArrow, n; leftArrow = left; rightArrow = right; n=left - right + 1; pivot = list[(left + right) / 2]; do { Total efficiency while (list[rightArrow] > pivot) --rightArrow; while (list[leftArrow] < pivot) ++leftArrow; if (leftArrow <= rightArrow) { Swap_Data(list[leftArrow], list[rightArrow]); ++leftArrow; --rightArrow; level++; printf(“nLevel %d : ”,level); efficiency(n); } } while (rightArrow >= leftArrow); if (left < rightArrow) QuickSort(list, left, rightArrow); if (leftArrow < right) QuickSort(list, leftArrow, right); }
  • 8. Since each element ultimately ends up in the correct position, the algorithm correctly sorts. But how long does it take?.On this basis it is divided into following three cases. 1. Best Case 2. Worst Case 3. Average Case Best Case for Quick Sort The best case for divide-and-conquer algorithms comes when we split the input as evenly as possible. Thus in the best case, each sub problem is of size n/2. The partition step on each sub problem is linear in its size. the total efficiency(time taken) in the best case is O(nlog2n). Worst Case for Quicksort Suppose instead our pivot element splits the array as unequally as possible. Thus instead of n/2 elements in the smaller half, we get zero, meaning that the pivot element is the biggest or smallest element in the array. The Average Case for Quicksort Suppose we pick the pivot element at random in an array of n keys Half the time, the pivot element will be from the centre half of the sorted array. Whenever the pivot element is from positions n/4 to 3n/4, the larger remaining sub-array contains at most 3n/4 elements.