We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Day 20: Sorting
Day 20: Sorting
+ 1 comment I have been using this 30 days of code challenge fairly successfully to both practice and learn things that were missed in my education. I do not have a CS undergraduate, but have taken some coding classes. I am confused why today the tutorial has to be "unlocked" and if it is you get 0 points for solving the challenge. In the discussions it seems as thought the tutorial maybe used to be wrong or point to the wrong things anyway, but seriously, block learning when people can obviously cheat by looking at the discussions anways.
+ 1 comment !/bin/python3
import math import os import random import re import sys
if name == 'main': n = int(input().strip())
a = list(map(int, input().rstrip().split())) # Write your code here s=0 k=1 while k>0: k=0 for i in range(n-1): if a[i]>a[i+1]: a[i],a[i+1]=a[i+1],a[i] k+=1 s+=1 print("Array is sorted in {} swaps.".format(s)) print("First Element: {}".format(a[0])) print("Last Element: {}".format(a[-1]))
+ 0 comments Easy C# Solution
int numberOfSwaps = 0; for(int i = 0; i < n; i++) { for(int j = 0; j < n - 1; j++) { if(a[j] > a[j + 1]) { int temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; numberOfSwaps++; } } if(numberOfSwaps == 0) { break; } } Console.WriteLine($"Array is sorted in {numberOfSwaps} swaps."); Console.WriteLine($"First Element: {a[0]}"); Console.WriteLine($"Last Element: {a[a.Count - 1]}");
+ 0 comments JavaScript
function swap(a, j) { let temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; } let swapCount = 0 for (let i = 0; i < n; i++) { let numberOfSwaps = 0 for (let j = 0; j < n - 1; j++) { if (a[j] > a[j + 1]) { swap(a, j); swapCount++ numberOfSwaps++; } } if (!numberOfSwaps) break; } console.log(`Array is sorted in ${swapCount} swaps.`); console.log(`First Element: ${a[0]}`); console.log(`Last Element: ${a[a.length - 1]}`);
+ 0 comments java script
let total = 0 for(let i = 0;i<a.length;i++){ // let swap = 0 for(let j = 0;j< a.length-1;j++){ if(a[j]>a[j+1]){ let temp = a[j] a[j] = a[j+1] a[j+1] = temp total++ // swap++; } } // if(!swap)break; } let last = a.pop() console.log(`Array is sorted in ${total} swaps.`) console.log(`First Element: ${a[0]}`) console.log(`Last Element: ${last}`) }
Load more conversations
Sort 586 Discussions, By:
Please Login in order to post a comment