0% found this document useful (0 votes)
18 views9 pages

Vaudilla. Praktikum1

This document discusses quicksort algorithms and provides code examples for implementing iterative and recursive quicksort in C++. It includes code to generate test arrays, sort them using either approach, and output the results. The document is a lab assignment on algorithms and programming for computer science students.

Uploaded by

Adelbn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views9 pages

Vaudilla. Praktikum1

This document discusses quicksort algorithms and provides code examples for implementing iterative and recursive quicksort in C++. It includes code to generate test arrays, sort them using either approach, and output the results. The document is a lab assignment on algorithms and programming for computer science students.

Uploaded by

Adelbn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Modul 1 Makassar, 22 Mei 2021

PRAKTIKUM

ALGORITMA DAN PEMOGRAMAN 2

Nama : Vaudilla

Nim : 13020200017

Nama Asisten : 1. Muh. Syawal

2. Andi Ikram

PROGRAM STUDI TEKNIK INFORMATIKA

FAKULTAS ILMU KOMPUTER

UNIVERSITAS MUSLIM INDONESIA

MAKASSAR
A. Code program

#include <iostream>

using namespace std;

int partition_iterative (int arr[], int l, int h)

int x = arr[h];

int i = (l - 1);

for (int j = l; j <= h - 1; j++) {

if (arr[j] <= x) {

i++;

swap(arr[i], arr[j]);

swap(arr[i + 1], arr[h]);

return (i + 1);

void quick_sort(int arr[], int l, int h)

int stack[h-l+1];

int top = -1;

stack[++top]=l;

stack[++top]=h;

while(top >= 0)

h = stack[top--];

l = stack[top--];

int p = partition_iterative(arr, l, h);

if(p-1>l)

{
stack[++top] = l;

stack[++top] = p - 1;

if(p+1<h)

stack[++top] = p + 1;

stack[++top] = h;

int partition_rekursive (int a[], int start, int end) {

int pivot = a[end];

int partitionIndex = start;

for (int i=start; i<end; i++) {

if (a[i] < pivot) {

swap (a[i], a[partitionIndex]);

partitionIndex++;

swap (a[partitionIndex], a[end]);

return partitionIndex;

void quickSort (int a[], int start, int end) {

if (start < end) {

int partitionIndex = partition_iterative (a,start,end);

quickSort (a,start,partitionIndex-1);

quickSort (a,partitionIndex+1,end);

}
}

int main()

int n, pilih, cari, hasil;

cout << "Masukkan Kapasitas Array [] : " ; cin >> n ;

int a[n];

for (int x=0 ; x<=n-1 ; x++) {

cout << "Masukkan Nilai ke - " << x+1 << " : " ;

cin >> a[x];

cout << "Urutkan data : " <<endl;

cout << "1. iterative quick sort :" << endl;

cout << "2. recursive quick sort :" << endl;

cout << "Masukkan Pilihan [1..2] :" << endl; cin >> pilih;

cout<<endl;

if ( pilih == 1) {

cout << "Pengurutan data dengan iterative quick sort" << endl;

cout <<"========================================" <<endl;

cout << "Data sebelum di urutkan" << endl;

for(int x=0;x<n;x++)

cout << a[x] << " ";

cout << endl;

quick_sort(a,0,n-1);
cout << " Data setelah diurutkan " << endl;

for(int x=0;x<n;x++)

cout<<a[x]<<" ";

cout<<endl;

else if ( pilih == 2) {

cout << "Pengurutan data dengan rekursive quick sort" << endl;

cout <<"========================================" <<endl;

cout << "Data sebelum di urutkan" << endl;

for(int x=0;x<n;x++)

cout << a[x] << " ";

cout << endl;

quickSort (a,0,n-1);

cout << " Data setelah diurutkan " << endl;

for(int x=0;x<n;x++)

cout<<a[x]<<" ";

cout<<endl;

}
B. Output

You might also like