1 / 8

METODA BULELOR (BUBBLE SORT)

METODA BULELOR (BUBBLE SORT). Elev ă : Borhidan Cristina. Enun ţ ul problemei. Se d ă un vector format din elemente numere reale. Se cere s ă se ordoneze în ordine crescătoare şirul dat, folosind metoda bulelor (bubble sort). Exemplu:. t = 1. t =2. n=5 0.0 1.1 1.0 1.2 0.08.

ehren
Télécharger la présentation

METODA BULELOR (BUBBLE SORT)

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. METODA BULELOR(BUBBLE SORT) Elevă: Borhidan Cristina

  2. Enunţul problemei Se dă un vector format din elemente numere reale. Se cere să se ordoneze în ordine crescătoare şirul dat, folosind metoda bulelor (bubble sort).

  3. Exemplu: t = 1 t =2 n=5 0.0 1.1 1.0 1.2 0.08 0.0 > 1.1 ? NU 1.1 > 1.0 ? DA 0.0 > 1.0 ? NU 1.0 > 1.1 ? NU 1.1 > 0.08 ? DA 1.1 > 1.2 ? NU 1.2 > 0.08 ? DA

  4. t = 3 t = 4 0.0 > 1.0 ? NU 1.0 > 0.08 ? DA 0.0 > 0.08 ? NU

  5. Algoritm Pasul 1:Se compară două câte două elemente consecutive, iar în cazul în care acestea nu se află în relaţia dorită (în ordine crescătoare sau descrescătoare) ele vor fi interschimbate. Pasul 2: Procesul de comparare se va încheia când toate perechile de elemente consecutive sunt în relaţia de ordine dorită.

  6. Implementare A -şirul de numere n - reprezintă numarul de elemente pe care il conţine şirul t - traversă

  7. Program: Program sortare_prin_metoda_bulelor; Type vector=array [1..100] of integer; Var A:vector; n,i:integer; Procedure bubble_sort (var a:vector; n:integer); Var t: integer; aux: real; ok: boolean; Begin t:=1; Repeat ok := true; for i:= 1 to n-t do if a[i] > a[i+1] then begin ok:= false; aux:= a[i]; a[i]:= a[i+1]; a[i+1]:= aux; end; t:= t+1; Until ok ; End;

  8. BEGIN write (‘n=‘); readln (n); for i:= 1 to n do begin write (‘a[‘,i,’]=‘); readln (a[i]); end; bubble_sort (a,n); writeln (‘elementele in ordine crescatoare : ‘); for i := 1 to n do writeln (a[i]); readln; END.

More Related