1 / 8

Ordenamientos

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

Download Presentation

Ordenamientos

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Ordenamientos En arreglos

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

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

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

  5. ShellSort public void shellSort() { int inner, outer; double temp; int h = 1; // find initial value of h while(h <= nElems/3) h = h*3 + 1; // (1, 4, 13, 40, 121, ...) while(h>0) // decreasing h, until h=1 { // h-sort the file for(outer=h; outer<nElems; outer++) { temp = theArray[outer]; inner = outer;

  6. Continua Shell // one subpass (eg 0, 4, 8) while(inner > h-1 && theArray[inner-h] >= temp) { theArray[inner] = theArray[inner-h]; inner -= h; } theArray[inner] = temp; } // end for h = (h-1) / 3; // decrease h } // end while(h>0) } // end shellSort()

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

  8. public int partitionIt(int left, int right, double pivot) { int leftPtr = left-1; // left (after ++) int rightPtr = right; // right-1 (after --) while(true) { // find bigger item while( theArray[++leftPtr] < pivot ) ; // (nop) // find smaller item while(rightPtr > 0 && theArray[--rightPtr] > pivot) ; // (nop) if(leftPtr >= rightPtr) // if pointers cross, break; // partition done else // not crossed, so swap(leftPtr, rightPtr); // swap elements } // end while(true) swap(leftPtr, right); // restore pivot return leftPtr; // return pivot location }

More Related