From 4919c57c40ae3464d0782b9a989445b31f40463c Mon Sep 17 00:00:00 2001 From: ashishpal25 <169115421+ashishpal25@users.noreply.github.com> Date: Fri, 18 Oct 2024 13:15:06 +0530 Subject: [PATCH] Create bubble Sort --- bubble Sort | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 bubble Sort diff --git a/bubble Sort b/bubble Sort new file mode 100644 index 000000000..45f06ab63 --- /dev/null +++ b/bubble Sort @@ -0,0 +1,23 @@ +function bubbleSort(arr) { + const n = arr.length; + + + for (let i = 0; i < n - 1; i++) { + + for (let j = 0; j < n - i - 1; j++) { + + if (arr[j] > arr[j + 1]) { + + const temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + } + } + } + return arr; +} + + +const array = [64, 34, 25, 12, 22, 11, 90]; +const sortedArray = bubbleSort(array); +console.log('Sorted Array:', sortedArray);