Ordenamientos

8
Ordenamientos Ordenamientos En arreglos En arreglos

description

Ordenamientos. En arreglos. Burbuja. public void bubbleSort() { int out, in; for(out=nElems-1; out>1; out--) for(in=0; in a[in+1] ) swap(in, in+1); } // end bubbleSort(). Inserción. - PowerPoint PPT Presentation

Transcript of Ordenamientos

Page 1: Ordenamientos

Ordenamientos Ordenamientos

En arreglosEn arreglos

Page 2: Ordenamientos

BurbujaBurbujapublic void bubbleSort()public void bubbleSort() {{ int out, in;int out, in; for(out=nElems-1; out>1; out--) for(out=nElems-1; out>1; out--) for(in=0; in<out; in++) for(in=0; in<out; in++) if( a[in] > a[in+1] ) if( a[in] > a[in+1] ) swap(in, in+1);swap(in, in+1); } // end bubbleSort()} // end bubbleSort()

Page 3: Ordenamientos

InserciónInserciónpublic void insertionSort()public void insertionSort() {{ int in, out;int in, out; for(out=1; out<nElems; out++) for(out=1; out<nElems; out++) {{ double temp = a[out]; double temp = a[out]; in = outin = out while(in>0 && a[in-1] >= temp) while(in>0 && a[in-1] >= temp) {{ a[in] = a[in-1]; a[in] = a[in-1]; --in; --in; }} a[in] = temp; a[in] = temp; } // end for} // end for} // end insertionSort()} // end insertionSort()

Page 4: Ordenamientos

SelecciónSelecciónpublic void selectionSort()public void selectionSort() {{ int out, in, min;int out, in, min; for(out=0; out<nElems-1; out++) for(out=0; out<nElems-1; out++) {{ min = out; min = out; for(in=out+1; in<nElems; in++) for(in=out+1; in<nElems; in++) if(a[in] < a[min] ) if(a[in] < a[min] ) min = in; min = in; swap(out, min); swap(out, min); } // end for(outer)} // end for(outer)} // end selectionSort()} // end selectionSort()

Page 5: Ordenamientos

ShellSortShellSort public void shellSort()public void shellSort() {{ int inner, outer;int inner, outer; double temp;double temp;

int h = 1; // find initial value of hint h = 1; // find initial value of h while(h <= nElems/3)while(h <= nElems/3) h = h*3 + 1; // (1, 4, 13, 40, 121, ...)h = h*3 + 1; // (1, 4, 13, 40, 121, ...)

while(h>0) // decreasing h, until h=1while(h>0) // decreasing h, until h=1 {{ // h-sort the file// h-sort the file for(outer=h; outer<nElems; outer++)for(outer=h; outer<nElems; outer++) {{ temp = theArray[outer];temp = theArray[outer]; inner = outer;inner = outer;

Page 6: Ordenamientos

Continua ShellContinua Shell// one subpass (eg 0, 4, 8)// one subpass (eg 0, 4, 8) while(inner > h-1 && theArray[inner-h] while(inner > h-1 && theArray[inner-h]

>= temp)>= temp) {{ theArray[inner] = theArray[inner-h];theArray[inner] = theArray[inner-h]; inner -= h;inner -= h; }} theArray[inner] = temp;theArray[inner] = temp; } // end for} // end for h = (h-1) / 3; // decrease hh = (h-1) / 3; // decrease h } // end while(h>0)} // end while(h>0) } // end shellSort()} // end shellSort()

Page 7: Ordenamientos

QuickSortQuickSortpublic void recQuickSort(int left, int right)public void recQuickSort(int left, int right) {{ if(right-left <= 0) // if size <= 1,if(right-left <= 0) // if size <= 1, return; // already sortedreturn; // already sorted else // size is 2 or largerelse // size is 2 or larger {{ double pivot = theArray[right]; // rightmost itemdouble pivot = theArray[right]; // rightmost item // partition range// partition range int partition = partitionIt(left, right, pivot);int partition = partitionIt(left, right, pivot); recQuickSort(left, partition-1); // sort left siderecQuickSort(left, partition-1); // sort left side recQuickSort(partition+1, right); // sort right siderecQuickSort(partition+1, right); // sort right side }} } // end recQuickSort() } // end recQuickSort()

Page 8: Ordenamientos

public int partitionIt(int left, int right, double pivot)public int partitionIt(int left, int right, double pivot) {{ int leftPtr = left-1; // left (after ++)int leftPtr = left-1; // left (after ++) int rightPtr = right; // right-1 (after --)int rightPtr = right; // right-1 (after --) while(true)while(true) { // find bigger item{ // find bigger item while( theArray[++leftPtr] < pivot )while( theArray[++leftPtr] < pivot ) ; // (nop); // (nop) // find smaller item// find smaller item while(rightPtr > 0 && theArray[--rightPtr] > pivot)while(rightPtr > 0 && theArray[--rightPtr] > pivot) ; // (nop); // (nop)

if(leftPtr >= rightPtr) // if pointers cross,if(leftPtr >= rightPtr) // if pointers cross, break; // partition donebreak; // partition done else // not crossed, soelse // not crossed, so swap(leftPtr, rightPtr); // swap elementsswap(leftPtr, rightPtr); // swap elements } // end while(true)} // end while(true) swap(leftPtr, right); // restore pivotswap(leftPtr, right); // restore pivot return leftPtr; // return pivot locationreturn leftPtr; // return pivot location }}