xxxxxxxxxx
if (!file_exists($file_path)) {
unlink($file_path);
}
xxxxxxxxxx
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int x = time(0);
srand(x);
const int size = 10;
int arr[size];
for (int i = 0; i < size; i++)
{
arr[i] = (rand() % 10) + 1;
}
for (int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
for (int i = 1; i < size; i++)
{
int temp;
if (arr[i] < arr[i - 1])
{
temp = arr[i];
int j = i;
do
{
arr[j] = arr[j - 1];
j--;
} while (j > 0 && arr[j - 1] > temp);
arr[j] = temp;
}
}
cout << endl;
for (int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
return 0;
}
xxxxxxxxxx
{% assign sorted_products = collection.products | sort: SELECT_CHOICE_HERE %}
xxxxxxxxxx
<div class="sort-collection-by">
<label for="sort-by">Sort by</label>
<select id="sort-by">
<option value="manual">Featured</option>
<option value="price-ascending">Price: Low to High</option>
<option value="price-descending">Price: High to Low</option>
<option value="title-ascending">A-Z</option>
<option value="title-descending">Z-A</option>
<option value="created-ascending">Oldest to Newest</option>
<option value="created-descending">Newest to Oldest</option>
<option value="best-selling">Best Selling</option>
</select>
</div>
xxxxxxxxxx
let numbers = [1, 4, 1, 2, 7, 5, 2];
let count = [];
let output =[];
let i = 0;
let max = Math.max(numbers);
// initialize counter
for (i = 0; i <= max; i++) {
count[i] = 0;
}
// initialize output array
for (i = 0; i < numbers.length; i++) {
output[i] = 0;
}
for (i = 0; i < numbers.length; i++) {
count[numbers[i]]++;
}
for (i = 1; i < count.length; i++) {
count[i] += count[i-1];
}
for (i = numbers.length - 1; i >= 0; i--) {
output[--count[numbers[i]]] = numbers[i];
}
// output sorted array
for (i = 0; i < output.length; i++) {
console.log(output[i]);
}
xxxxxxxxxx
(function(){
var arrayNumb = [2, 8, 15, 16, 23, 42];
arrayNumb.sort();
console.log(arrayNumb);
})();
xxxxxxxxxx
jQuery('#sort-by').bind('change', function() {
Shopify.queryParams.sort_by = jQuery(this).val();
location.search = jQuery.param(Shopify.queryParams).replace(/\+/g, '%20');
});
xxxxxxxxxx
#Implement, time & rank 13 sorting algorithms in python
#https://github.com/paulnegz/sorts-py/tree/main
def quick_sort(array :list)->list:
def _quick_sort(array :list)->list:
if len(array)<=1: return array
pivot, is_bigger = array.pop(), lambda x: x>pivot
left_partition = _quick_sort([x for x in array if not is_bigger(x)])
right_partition = _quick_sort([x for x in array if is_bigger(x)])
return [*left_partition, pivot, *right_partition]
return _quick_sort(array)
def bubble_sort(array :list)->list:
for y in range(1, len(array)):
swap: bool = False
for x in range(1,len(array)-(y-1)):
if array[x]<array[x-1]:
array[x],array[x-1], swap = array[x-1],array[x], True
if not swap: break
return array
def radix_sort(array :list)->list:
base, max_length = 10, len(str(max(array)))
for n in range(max_length):
bucket=[[] for _ in range(base)]
for item in array: bucket[item//base**n%base].append(item)
flat_map = lambda arr: reduce(lambda acc, curr: [*acc,*curr], arr)
array = flat_map(bucket)
return array
def selection_sort(array :list)->list:
for idx_y, _ in enumerate(array):
min_idx = idx_y
for idx in range(idx_y,len(array)):
if array[idx] < array[min_idx]: min_idx = idx
array[min_idx], array[idx_y] = array[idx_y], array[min_idx]
return array
def _shell_sort(array: list)->list:
LENGTH, gap= len(array), len(array)//2
while gap>0:
for j in range(gap, LENGTH):
i=j-gap
while i>=0:
if array[i+gap]>array[i]: break
else: array[i+gap],array[i]=array[i],array[i+gap]
i=i-gap
gap=gap//2
return array
def bucket_sort(array :list)->list:
BUCKET_SIZE = ceil(len(array)**(3/4))
min_val, max_val = min(array), max(array)
RANGE = (max_val-min_val+1)/BUCKET_SIZE
bucket = [[] for _ in range(BUCKET_SIZE)]
get_idx = lambda num: int((num-min_val)/RANGE)
for num in array: bucket[get_idx(num)].append(num)
for x in bucket: _insertion_sort(x)
flat_map = lambda arr: reduce(lambda acc, curr: [*acc,*curr], arr)
return flat_map(bucket)