{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def fusion(L, bas, milieu, haut):\n",
    "    '''Entree: liste L t.q. L[bas:milieu+1] et \n",
    "            L[milieu+1:haut+1] sont triees\n",
    "    Trie L[bas:haut+1]'''\n",
    "    L1 = L[bas:milieu+1]\n",
    "    L2 = L[milieu+1:haut+1]\n",
    "    print(\"on fusionne les sous-listes\", L1, \"et\", L2, end=\": \")\n",
    "\n",
    "    L1.append(float('inf'))\n",
    "    L2.append(float('inf'))\n",
    "    L1_index = 0\n",
    "    L2_index = 0  \n",
    "    for i in range(bas, haut+1):\n",
    "        if L1[L1_index] <= L2[L2_index]:\n",
    "            L[i] = L1[L1_index]\n",
    "            L1_index += 1\n",
    "        else:\n",
    "            L[i] = L2[L2_index]\n",
    "            L2_index += 1\n",
    "    \n",
    "    print(\"L =\", L)\n",
    "            \n",
    "            \n",
    "def tri_par_fusion(L, bas, haut):\n",
    "    '''\n",
    "    Entree: liste L, indices bas et haut\n",
    "    Trie la tranche de L entre bas et haut\n",
    "    '''\n",
    "    if haut - bas > 0:      \n",
    "        milieu = (bas + haut)//2   \n",
    "        print(\"on va trier la sous-liste\", L[bas:milieu+1])\n",
    "        tri_par_fusion(L, bas, milieu)\n",
    "        print(\"on va trier la sous-liste\", L[milieu+1:haut+1])\n",
    "        tri_par_fusion(L, milieu+1, haut)\n",
    "        fusion(L, bas, milieu, haut)\n",
    "    \n",
    "    else:\n",
    "        print(\"c'est une liste de taille 1.\")\n",
    "\n",
    "#L = [4, 3, 2, 1]\n",
    "#tri_par_fusion(L, 0, len(L)-1)\n",
    "#print(L)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
