-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubble-sort.js
24 lines (21 loc) · 5.48 KB
/
bubble-sort.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
var bubbleSort = function(arr){
var isSwapped = true;
for(var iterations = 1; iterations < arr.length && isSwapped; iterations++){
isSwapped = false;
for(var i = 1; i < arr.length; i++){
if(arr[i-1] > arr[i]){
var temp = arr[i-1];
arr[i-1] = arr[i];
arr[i] = temp;
isSwapped = true;
}
}
}
return arr;
}
//var input = [1,2,3,4,3,4,53,3,2,3,3223,34,2,32,645,34,368,99,5,46,7,8,7,9,8,67];
var input = [599, 636, 674, 764, 919, 597, 121, 381, 652, 636, 479, 412, 371, 760, 88, 65, 584, 761, 735, 156, 796, 479, 183, 761, 127, 781, 343, 29, 431, 53, 720, 320, 728, 25, 160, 460, 510, 404, 3, 468, 141, 606, 701, 449, 887, 994, 138, 43, 171, 208, 409, 229, 59, 626, 226, 115, 772, 677, 948, 37, 342, 308, 64, 247, 924, 235, 149, 917, 809, 919, 614, 657, 326, 252, 173, 830, 565, 209, 986, 472, 841, 29, 602, 514, 821, 18, 686, 557, 102, 20, 103, 399, 730, 481, 631, 446, 369, 540, 217, 993, 701, 423, 477, 444, 527, 737, 677, 307, 632, 459, 861, 919, 915, 9, 686, 414, 905, 427, 992, 662, 33, 476, 254, 966, 162, 669, 730, 158, 830, 881, 848, 335, 355, 906, 705, 205, 651, 580, 998, 391, 221, 532, 443, 247, 132, 88, 249, 793, 130, 686, 286, 476, 11, 136, 684, 909, 838, 599, 994, 617, 782, 45, 743, 791, 675, 612, 23, 532, 328, 602, 473, 665, 615, 726, 656, 584, 15, 88, 163, 727, 218, 319, 798, 400, 552, 354, 841, 770, 627, 138, 192, 362, 273, 760, 404, 865, 48, 809, 405, 964, 341, 351, 731, 726, 177, 664, 700, 275, 611, 840, 506, 254, 838, 395, 884, 719, 328, 495, 348, 707, 934, 368, 607, 869, 844, 806, 904, 893, 155, 868, 169, 73, 554, 414, 56, 511, 19, 935, 615, 805, 67, 341, 588, 216, 355, 967, 968, 373, 319, 360, 580, 367, 27, 27, 674, 792, 642, 707, 952, 101, 575, 261, 495, 437, 890, 401, 766, 537, 590, 573, 751, 781, 353, 381, 757, 974, 299, 82, 893, 571, 229, 694, 674, 246, 253, 918, 855, 708, 540, 554, 396, 193, 504, 494, 826, 476, 320, 936, 681, 273, 792, 984, 376, 328, 660, 921, 812, 427, 794, 421, 312, 498, 596, 327, 474, 551, 896, 369, 759, 32, 309, 83, 232, 909, 777, 475, 445, 325, 274, 258, 267, 392, 992, 679, 746, 44, 477, 868, 361, 323, 813, 177, 770, 790, 410, 600, 634, 773, 740, 334, 837, 755, 692, 164, 728, 261, 675, 670, 591, 191, 261, 51, 261, 596, 61, 128, 797, 460, 150, 703, 587, 844, 43, 918, 29, 453, 995, 219, 891, 413, 94, 588, 132, 133, 834, 891, 436, 323, 660, 43, 330, 129, 416, 901, 367, 855, 394, 210, 513, 474, 98, 753, 954, 135, 739, 452, 657, 343, 928, 62, 433, 351, 134, 646, 832, 698, 312, 441, 681, 230, 252, 288, 19, 795, 355, 19, 33, 127, 800, 962, 362, 152, 472, 258, 559, 472, 164, 830, 382, 235, 586, 164, 11, 46, 599, 292, 538, 904, 638, 884, 66, 771, 771, 899, 155, 848, 625, 239, 44, 420, 906, 194, 771, 481, 94, 219, 461, 692, 926, 313, 537, 782, 539, 189, 927, 790, 105, 667, 301, 343, 538, 721, 744, 18, 283, 29, 497, 224, 182, 867, 671, 256, 66, 551, 428, 562, 744, 322, 662, 407, 255, 387, 78, 176, 799, 340, 136, 781, 663, 945, 302, 817, 154, 346, 482, 882, 801, 219, 680, 926, 342, 694, 477, 481, 429, 366, 168, 119, 454, 945, 446, 388, 907, 843, 710, 150, 796, 696, 983, 419, 712, 776, 25, 420, 247, 440, 9, 925, 621, 708, 989, 86, 225, 470, 142, 935, 128, 325, 144, 268, 432, 295, 196, 944, 696, 213, 893, 819, 361, 408, 121, 954, 794, 327, 411, 623, 29, 693, 881, 724, 845, 705, 146, 3, 700, 943, 382, 443, 718, 52, 306, 7, 421, 314, 331, 382, 575, 12, 643, 998, 160, 391, 435, 208, 218, 671, 168, 943, 438, 698, 254, 658, 993, 700, 665, 996, 844, 938, 532, 755, 552, 92, 60, 840, 744, 667, 707, 697, 398, 910, 344, 458, 636, 132, 61, 511, 410, 81, 838, 804, 706, 950, 238, 173, 646, 185, 921, 151, 638, 176, 913, 15, 790, 433, 722, 254, 261, 522, 948, 653, 80, 111, 62, 897, 333, 288, 939, 265, 779, 741, 980, 441, 537, 570, 664, 575, 642, 413, 109, 553, 716, 198, 271, 852, 465, 461, 254, 6, 172, 428, 884, 831, 149, 927, 116, 991, 576, 557, 928, 689, 547, 456, 233, 716, 222, 468, 790, 287, 807, 349, 800, 992, 330, 386, 291, 795, 339, 55, 452, 873, 147, 367, 549, 761, 363, 113, 360, 83, 398, 445, 950, 414, 539, 303, 319, 56, 62, 439, 230, 396, 337, 551, 249, 154, 40, 558, 397, 190, 552, 882, 272, 173, 233, 334, 680, 124, 936, 2, 405, 724, 392, 736, 964, 798, 211, 261, 575, 206, 560, 160, 741, 928, 95, 658, 246, 516, 280, 26, 642, 319, 149, 24, 377, 860, 102, 322, 584, 546, 75, 204, 165, 358, 37, 297, 21, 142, 247, 626, 144, 998, 502, 195, 870, 149, 366, 664, 4, 155, 22, 923, 610, 103, 948, 715, 938, 220, 467, 993, 993, 991, 731, 29, 380, 669, 719, 55, 709, 489, 751, 411, 726, 317, 904, 313, 754, 813, 677, 512, 281, 814, 762, 43, 501, 905, 277, 449, 936, 671, 670, 962, 459, 215, 373, 33, 145, 540, 470, 656, 76, 22, 173, 312, 941, 829, 605, 377, 732, 856, 689, 685, 525, 57, 70, 946, 850, 917, 957, 288, 298, 709, 593, 978, 577, 251, 666, 330, 446, 102, 158, 530, 202, 927, 225, 476, 689, 351, 856, 112, 450, 536, 71, 898, 386, 463, 827, 849, 531, 107, 538, 386, 993, 581, 22, 813, 577, 777, 670, 302, 64, 686, 296, 333, 955, 507, 198, 886, 967, 86, 954, 716, 675, 195, 410, 355, 525, 30, 469, 490, 658, 313, 964, 179, 953, 837, 227, 579, 379, 810, 828, 7, 74, 483, 581, 530, 388, 578, 38, 552, 562, 676, 427, 776, 845, 753, 96, 927, 546, 928, 402, 707, 399, 124, 493, 669, 352, 807, 517, 375, 800, 929, 388, 848, 425, 323, 653, 496, 602, 815, 768, 429, 391, 989, 348, 457, 64, 315, 443, 398, 175, 911];
var startTime = new Date().getTime();
var output = bubbleSort(input);
var endTime = new Date().getTime();
console.log(output, 'bubbleSorted in : '+(endTime-startTime)+' milli seconds');