bubble sort
JavaScript
void bubbleSort(int arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)
for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
swap(&arr[j], &arr[j+1]);
} void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
// An optimized version of Bubble Sort
void bubbleSort(int arr[], int n)
{
int i, j;
bool swapped;
for (i = 0; i < n-1; i++)
{
swapped = false;
for (j = 0; j < n-i-1; j++)
{
if (arr[j] > arr[j+1])
{
swap(&arr[j], &arr[j+1]);
swapped = true;
}
}
// IF no two elements were swapped by inner loop, then break
if (swapped == false)
break;
}
} // Por ter uma complexidade alta,
// não é recomendado para um conjunto de dados muito grande.
// Complexidade: O(n²) / O(n**2) / O(n^2)
// @see https://www.youtube.com/watch?v=TZRWRjq2CAg
function bubblesort(list) {
const n = list.length;
for (let i = 0; i < n - 1; i += 1) {
for (let j = 0; j < n - 1; j += 1) {
if (list[j] > list[j + 1]) {
[list[j], list[j + 1]] = [list[j + 1], list[j]];
}
}
}
return list;
}
bubblesort([4, 7, 2, 6, 4, 1, 8, 3]);# Python program for implementation of Bubble Sort
def bubbleSort(arr):
n = len(arr)
# Traverse through all array elements
for i in range(n):
# Last i elements are already in place
for j in range(0, n-i-1):
# traverse the array from 0 to n-i-1
# Swap if the element found is greater
# than the next element
if arr[j] > arr[j+1] :
arr[j], arr[j+1] = arr[j+1], arr[j]
# Driver code to test above
arr = [64, 34, 25, 12, 22, 11, 90]
bubbleSort(arr)
print ("Sorted array is:")
for i in range(len(arr)):
print ("%d" %arr[i]),
Also in JavaScript:
- Title
- javascript add commas to number
- Category
- JavaScript
- Title
- express route parameters
- Category
- JavaScript
- Title
- delete with unlinksync node
- Category
- JavaScript
- Title
- how to check an empty array js
- Category
- JavaScript
- Title
- express img folder
- Category
- JavaScript
- Title
- how to update angular version
- Category
- JavaScript
- Title
- how to attach javascript to html
- Category
- JavaScript
- Title
- javascript array pop
- Category
- JavaScript
- Title
- how to add element in json object
- Category
- JavaScript
- Title
- delegate jquery
- Category
- JavaScript
- Title
- deno
- Category
- JavaScript
- Title
- get start of day javascript
- Category
- JavaScript
- Title
- get current directory vbscript
- Category
- JavaScript
- Title
- javascript change web page title
- Category
- JavaScript
- Title
- concat object
- Category
- JavaScript
- Title
- how to make one line if in js
- Category
- JavaScript
- Title
- global site tag (gtag.js) - google analytics gatsby
- Category
- JavaScript
- Title
- fizzbuzz js
- Category
- JavaScript
- Title
- click doesn't work on ajax loaded data
- Category
- JavaScript
- Title
- const{}=
- Category
- JavaScript
- Title
- get all data attributes jquery from multiple elements
- Category
- JavaScript
- Title
- iterate array in javascrpt
- Category
- JavaScript
- Title
- how to check if a number is even or odd in javascript
- Category
- JavaScript
- Title
- Cannot set property 'innerHTML' of null
- Category
- JavaScript
- Title
- create dice game javascript
- Category
- JavaScript
- Title
- chart.js radar chart
- Category
- JavaScript
- Title
- html parser javascript
- Category
- JavaScript
- Title
- get month in two digit in javascript date
- Category
- JavaScript
- Title
- html inline javascript
- Category
- JavaScript
- Title
- add select option jquery
- Category
- JavaScript
- Title
- angular material theme colors
- Category
- JavaScript
- Title
- array push javascript example
- Category
- JavaScript
- Title
- int cating javascript
- Category
- JavaScript
- Title
- How to check whether a checkbox is checked in jQuery?
- Category
- JavaScript
- Title
- how to make a show password button
- Category
- JavaScript
- Title
- dynamic loaded content can't find library jqury
- Category
- JavaScript
- Title
- Elements in iteration expect to have 'v-bind:key' directives
- Category
- JavaScript
- Title
- bootstrap datepicker js cdn
- Category
- JavaScript
- Title
- how to add multiple comment in react
- Category
- JavaScript
- Title
- dynsmic calss in react add
- Category
- JavaScript
- Title
- bootstrap checkbox
- Category
- JavaScript
- Title
- how to close another browser tab with javascript
- Category
- JavaScript
- Title
- iterate over object javascript
- Category
- JavaScript
- Title
- Javascript clear canvas
- Category
- JavaScript
- Title
- git empty commit
- Category
- JavaScript
- Title
- how to get textedit on mac without download
- Category
- JavaScript
- Title
- How to get the path to the file that required your module?
- Category
- JavaScript
- Title
- convert object to json javascript
- Category
- JavaScript
- Title
- Angular watching for changes in $http.pendingRequests from directive
- Category
- JavaScript
- Title
- convert boolean to string javascript
- Category
- JavaScript
- Title
- devexpress dxdatagrid custom text field
- Category
- JavaScript
- Title
- how to assign same value of a key to 2 different keys in output JSON jolt
- Category
- JavaScript
- Title
- express get params after ?
- Category
- JavaScript
- Title
- array vowels js
- Category
- JavaScript
- Title
- get text of selected option jquery
- Category
- JavaScript
- Title
- clear input field value javascript
- Category
- JavaScript
- Title
- electron jquery
- Category
- JavaScript
- Title
- get last item in array js
- Category
- JavaScript
- Title
- expo open app settings
- Category
- JavaScript
- Title
- how to return a string x amount in javascript without using . repeat
- Category
- JavaScript
- Title
- export function javascript
- Category
- JavaScript
- Title
- JavaScript banner
- Category
- JavaScript
- Title
- isogram
- Category
- JavaScript
- Title
- foreach and replace item based on condition
- Category
- JavaScript
- Title
- how to verify timestamp format in javascript
- Category
- JavaScript
- Title
- installing vue router & implementation
- Category
- JavaScript
- Title
- deprecationwarning: mongoose
- Category
- JavaScript
- Title
- custom status discord bot
- Category
- JavaScript
- Title
- check window resize javascript
- Category
- JavaScript
- Title
- get the size of the browser jquery
- Category
- JavaScript
- Title
- how to access the second class of an element javascript
- Category
- JavaScript
- Title
- how to add border to a text in html with javascript
- Category
- JavaScript
- Title
- event.stoppropagation
- Category
- JavaScript
- Title
- how to play sound on load js
- Category
- JavaScript
- Title
- async await javascript push
- Category
- JavaScript
- Title
- get string of element
- Category
- JavaScript
- Title
- angular cli skip-tests
- Category
- JavaScript
- Title
- datatable on error.dt
- Category
- JavaScript
- Title
- custom processing datatables
- Category
- JavaScript
- Title
- How to append to a file in Node?
- Category
- JavaScript
- Title
- combine two arrays javascript
- Category
- JavaScript
- Title
- get screen width javascript
- Category
- JavaScript
- Title
- delete previous line node
- Category
- JavaScript
- Title
- boucle for of javascript
- Category
- JavaScript
- Title
- how to create a list in javascript
- Category
- JavaScript
- Title
- excel javascript api protect worksheet
- Category
- JavaScript
- Title
- add item to array javascript
- Category
- JavaScript
- Title
- get the size of the screen javascript
- Category
- JavaScript
- Title
- jason rpc reactjs
- Category
- JavaScript
- Title
- get timer start adding the started at time js
- Category
- JavaScript
- Title
- crypto.com api
- Category
- JavaScript
- Title
- check array values equal js
- Category
- JavaScript
- Title
- ajax .put
- Category
- JavaScript
- Title
- array.splice javascript
- Category
- JavaScript
- Title
- javascript 1 + "1"
- Category
- JavaScript
- Title
- how to use js console log
- Category
- JavaScript
- Title
- how to create duplicate key array in javascript
- Category
- JavaScript
- Title
- How to check if a member has a role discord.js
- Category
- JavaScript
- Title
- append to array js
- Category
- JavaScript
- Title
- Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project electronicbookshop: Compilation failure
- Category
- JavaScript