From ef72ba257c2be9c8167b74976cfe106bdeb53afe Mon Sep 17 00:00:00 2001 From: lfainsin Date: Wed, 23 Dec 2020 15:16:37 +0000 Subject: [PATCH] quicksort fonctionne git-svn-id: http://cregut.svn.enseeiht.fr/2020/1sn/pim/projets/GH-05@210350 e13453a9-b01f-0410-a051-f404c4f0c485 --- src/test_tri.adb | 26 ++++++++++-------- src/vector.adb | 71 +++++++++++++++++++++++++++--------------------- src/vector.ads | 3 +- 3 files changed, 57 insertions(+), 43 deletions(-) diff --git a/src/test_tri.adb b/src/test_tri.adb index 9901495..4411037 100644 --- a/src/test_tri.adb +++ b/src/test_tri.adb @@ -1,20 +1,20 @@ with Ada.Text_IO; use Ada.Text_IO; +-- with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; with Vector; -procedure pageRank is +procedure test_tri is Type T_Double is digits 3; - N: Natural := 10; + N: constant Natural := 10; package Vector_Double is new Vector(T_Element => T_Double, - N => N); + N => N); use Vector_Double; vec: T_Vecteur_Element; - vec_index: T_Vecteur_Natural; begin @@ -29,11 +29,15 @@ begin vec(8) := 6.0; vec(9) := 6.0; - put_line("avant:"); - put(vec); - new_line; - quicksort(vec, vec_index, 0, N-1); - put_line("après:"); - put(vec); -end pageRank; + new_line; + + put_line("avant:"); + put(vec); new_line; + + quicksort(vec, 0, N-1); + + put_line("après:"); + put(vec); new_line; + +end test_tri; diff --git a/src/vector.adb b/src/vector.adb index 84cc796..c9454b8 100644 --- a/src/vector.adb +++ b/src/vector.adb @@ -74,40 +74,49 @@ package body vector is end loop; end sort_insert_desc; - procedure quicksort(vec: in out T_Vecteur_Element; vec_index: in out T_Vecteur_Natural; a, b: in Natural) is - pivot: Natural := a; - i: Natural := a; - tmp: T_Element; + procedure quicksort(vec: in out T_Vecteur_Element; low, high: Natural) is + + procedure swap(left, right: Natural) is + tmp : constant T_Element := vec(left); + begin + vec(left) := vec(right); + vec(right) := tmp; + end swap; + begin - if b - a > 1 then - for j in a+1..b loop - if vec(j) <= vec(pivot) then - i := i + 1; - tmp := vec(i); - vec(i) := vec(j); - vec(j) := tmp; - else - null; + if high - low > 0 then + declare + pivot : constant T_Element := vec(low); + right : Natural := high; + left : Natural := low; + begin + loop + while left < right and not(pivot < vec(left)) loop + left := left + 1; + end loop; + while pivot < vec(right) loop + right := right - 1; + end loop; + + exit when right <= left; + swap(left, right); + left := left + 1; + right := right - 1; + end loop; + + if right = high then + right := right - 1; + swap(low, high); + end if; + + if left = low then + left := left - 1; end if; - end loop; - tmp := vec(i); - vec(i) := vec(pivot); - vec(pivot) := tmp; - - pivot := i; - - put(vec); new_line; - - if pivot - 1 > 0 then - quicksort(vec, vec_index, a, pivot-1); - elsif b - pivot - 1 > 0 then - quicksort(vec, vec_index, pivot+1, b); - else - null; - end if; - else - null; + --put(left); put(right); put(low); put(high); new_line; + quicksort(vec, low, right); + quicksort(vec, left, high); + end; end if; end quicksort; diff --git a/src/vector.ads b/src/vector.ads index 4f983de..05fbe9e 100644 --- a/src/vector.ads +++ b/src/vector.ads @@ -25,6 +25,7 @@ package Vector is procedure put(file: in out Ada.Text_IO.File_Type; vec: in T_Vecteur_Natural); procedure sort_insert_desc(vec: in out T_Vecteur_Element; vec_index: in out T_Vecteur_Natural); - procedure quicksort(vec: in out T_Vecteur_Element; vec_index: in out T_Vecteur_Natural; a, b: in Natural); + procedure quicksort(vec: in out T_Vecteur_Element; low, high: Natural); + end Vector;