From Clomosy Docs

No edit summary
No edit summary
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
Quick Sort, like Merge Sort, works on the divide and conquer principle. A pivot element is selected in the array, which serves as a reference point. Values smaller than the pivot are moved to its left, and values larger than the pivot are moved to its right. Then, the same process is applied recursively to the sub-arrays on the left and right of the pivot. As a result, the array becomes sorted.
Quick Sort, like Merge Sort, works on the divide and conquer principle. A pivot element is selected in the array, which serves as a reference point. Values smaller than the pivot are moved to its left, and values larger than the pivot are moved to its right. Then, the same process is applied recursively to the sub-arrays on the left and right of the pivot. As a result, the array becomes sorted.<br>


[[File:QuickSort1.mp4|frameless|500px]]<br>
[[File:QuickSort1.mp4|frameless|500px]]<br>


Steps:
Steps:
*Select a pivot element.
*Select a pivot element.
*Use the pivot to partition the dataset into two sub-arrays: elements smaller than the pivot and elements greater than the pivot.
*Use the pivot to partition the dataset into two sub-arrays: elements smaller than the pivot and elements greater than the pivot.
Line 10: Line 10:
*Combine the sub-arrays and the pivot.
*Combine the sub-arrays and the pivot.


'''Example:'''<br>
<b>Example</b><br>


:'''TRObject Syntax'''
<pre>
<pre>
   var
   var
Line 98: Line 97:
</pre>
</pre>


:'''Base Syntax'''
<h2> See Also </h2>
<pre>
  var
    arr: array[0..6] of Integer;
    i: Integer;
    str: string;
 
  procedure swap(a, b: Integer);
  var
    temp: Integer;
  begin
    temp := arr[a];
    arr[a] := arr[b];
    arr[b] := temp;
  end;
 
  function partition(low, high: Integer): Integer;
  var
    pivot, i, j: Integer;
  begin
    pivot := arr[high];
    i := low - 1;
   
    for j := low to high - 1 do
    begin
      if arr[j] < pivot then
      begin
        i := i + 1;
        swap(i, j);
      end;
    end;
   
    swap(i + 1, high);
    Result :=  i + 1;
  end;
 
 
  procedure quickSort(low, high: Integer);
  var
    pi: Integer;
  begin
    if low < high then
    begin
      pi := partition(low, high);
      quickSort(low, pi - 1);
      quickSort(pi + 1, high);
    end;
  end;
 
  begin
    arr[0] := 12;
    arr[1] := 11;
    arr[2] := 13;
    arr[3] := 5;
    arr[4] := 1;
    arr[5] := 7;
    arr[6] := 9;
    str := 'Unordered array: ';
   
    for i := 0 to Length(arr)-1 do
    begin
      str := str + IntToStr(arr[i]) + ' ';
    end; 
   
    ShowMessage(str);
 
    quickSort(0, Length(arr)-1);
    str := 'Array sorted from smallest to largest: ';
   
    for i := 0 to Length(arr)-1 do
    begin
      str := str + IntToStr(arr[i]) + ' ';
    end;
    ShowMessage(str);
 
    str := 'Array sorted from largest to smallest: '; 
   
    for i := Length(arr)-1 downto 0 do
    begin
      str := str + IntToStr(arr[i]) + ' ';
    end;
    ShowMessage(str);
  end;
 
</pre>
 
 
 
== See Also ==
* [[Sorting Algorithms]]
* [[Sorting Algorithms]]
* [[TclArray]]
* [[TclArray]]
* [[Arrays]]
* [[Arrays]]
{{#seo:|title=Quick Sort in Clomosy - Clomosy Docs}}
{{#seo:|description=Learn about Quick Sort in Clomosy. A guide to implementing this efficient sorting algorithm for faster data processing in your mobile apps.}}

Latest revision as of 12:57, 24 December 2024

Quick Sort, like Merge Sort, works on the divide and conquer principle. A pivot element is selected in the array, which serves as a reference point. Values smaller than the pivot are moved to its left, and values larger than the pivot are moved to its right. Then, the same process is applied recursively to the sub-arrays on the left and right of the pivot. As a result, the array becomes sorted.



Steps:

  • Select a pivot element.
  • Use the pivot to partition the dataset into two sub-arrays: elements smaller than the pivot and elements greater than the pivot.
  • Recursively sort the sub-arrays using Quick Sort.
  • Combine the sub-arrays and the pivot.

Example

  var
    arr: array[0..6] of Integer;
    i: Integer;
    str: string;
  
  void swap(a, b: Integer);
  var
    temp: Integer;
  {
    temp = arr[a];
    arr[a] = arr[b];
    arr[b] = temp;
  }
  
  function partition(low, high: Integer): Integer;
  var
    pivot, i, j: Integer;
  {
    pivot = arr[high];
    i = low - 1;
    
    for (j = low to high - 1)
    {
      if (arr[j] < pivot)
      {
        i = i + 1;
        swap(i, j);
      }
    }
    
    swap(i + 1, high);
    Result =  i + 1;
  }
  
  
  void quickSort(low, high: Integer);
  var
    pi: Integer;
  {
    if (low < high)
    {
      pi = partition(low, high);
      quickSort(low, pi - 1);
      quickSort(pi + 1, high);
    }
  }
  
  {
    arr[0] = 12;
    arr[1] = 11;
    arr[2] = 13;
    arr[3] = 5;
    arr[4] = 1;
    arr[5] = 7;
    arr[6] = 9;
    str = 'Unordered array: ';
    
    for (i = 0 to Length(arr)-1)
    {
      str = str + IntToStr(arr[i]) + ' ';
    }  
    
    ShowMessage(str);
  
    quickSort(0, Length(arr)-1);
    str = 'Array sorted from smallest to largest: ';
    
    for (i = 0 to Length(arr)-1)
    {
      str = str + IntToStr(arr[i]) + ' ';
    }
    ShowMessage(str);
  
    str = 'Array sorted from largest to smallest: ';  
    
    for (i = Length(arr)-1 downto 0)
    {
      str = str + IntToStr(arr[i]) + ' ';
    }
    ShowMessage(str);
  }

See Also