qsortcode.c - while (FirstUnknown <= L) { //consider the...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
#include <stdio.h> typedef int Item; /* Forward declaration of functions: */ Item ChoosePivot(Item A[],int F,int L); void Swap(Item A[],int F,int LastS1); void Partition(Item A[], int F, int L, int *PivotIndex); void QuickSort(Item A[], int F, int L) { int PivotIndex; if (F >= L) return; /* Choose a pivot item p from A[F. ..L] the items of A[F. ..L] about p */ Partition(A,F,L,&PivotIndex); /* the partition is A[. ..PivotIndex. ..L] */ QuickSort(A,F,PivotIndex-1); QuickSort(A,PivotIndex+1,L); } void Partition(Item A[], int F, int L, int *PivotIndex) { /*initialize Choose the pivot and swap it with A[F].*/ int p,LastS1, FirstUnknown; p = ChoosePivot(A,F,L); LastS1 = F; FirstUnknown = F + 1; //determine the regions S1 and S2
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: while (FirstUnknown <= L) { //consider the placement of the "leftmost" //item in the unknown region /*==> Here you will need to write code to move the data */ if (A[FirstUnknown] < p) { Swap(A,F,LastS1); LastS1++; FirstUnknown++; } else FirstUnknown++; } //place pivot in proper position between //S1 and S2, and mark its new location Swap(A,F,LastS1); *PivotIndex = LastS1; } void Swap(Item A,int F,int LastS1) { int temp; temp = A[F]; A[F] = A[LastS1]; A[LastS1] = temp; } void PrintArray(Item A,int F,int L) { while(F <= L) { printf("%d\n", A[F]); F++; } } Item ChoosePivot(Item A,int F,int L) { return A[F]; }...
View Full Document

This note was uploaded on 01/17/2012 for the course CSC 1254 taught by Professor Blanks,l during the Fall '08 term at LSU.

Page1 / 2

qsortcode.c - while (FirstUnknown <= L) { //consider the...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online