-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtask4.js
More file actions
91 lines (85 loc) · 2.12 KB
/
task4.js
File metadata and controls
91 lines (85 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Write a function which takes an array as input and sorts the array using bubble sort. Also print the array state as output on each pass
// function bubbleSort(array: number[]){
// sort logicc
// print state of array on each pass
// return sortedArray;
// }
//Raj-bhat
function bubblesort(){
var a=[3,2,1];
if(a.length>0){
for ( let i=0;i<a.length;i++){
for ( let j=0;j<a.length;j++){
var temp;
if(a[j]>a[j+1]){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
console.log(a);
}
}
}
else {
console.log("Invalid input");
}
}
bubblesort();
//Sachin
function sortArrayUsingBubbleSortTechnique(inpArr){
for(i = 0; i< inpArr.length-1; i ++ )
{
var swap = false; // To keep track if there are no swaps
for ( j = 0; j<inpArr.length-1;j++) {
if(inpArr[j] > inpArr[j+1]) {
swap =true;
let temp = inpArr[j+1];
inpArr[j+1]= inpArr[j] ;
inpArr[j]= temp;
}
}
if(swap===false)
{
break ;
}
console.log('after pass ' + i + ' ' + inpArr);
}
return(inpArr);
}
//Anusha
function bubbleSort (array){
for(let i=0;i<array.length-1;i++){
for(let j=0;j<array.length-1-i;j++){
if(array[j]>array[j+1]){
const temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
return array
}
console.log(bubbleSort([5,3,8,4,6]))
//Naveen
function bubbleSort(array) {
let isSwaped = true;
console.log(array);
for (var i = 0; i < array.length; i++) {
for (var j = 0; j < (array.length - i - 1); j++) {
(j == 0) ? console.log("Iteration " + i): null;
if (arr[j] > array[j + 1]) {
var temp = array[j];
array[j] = array[j + 1];
ararray = temp;
isSwaped = true;
}
console.log(array);
}
if (isSwaped === true) {
isSwaped = false;
} else {
return array;
}
}
return array;
}