Skip to content

Commit

Permalink
sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
RryLee committed Mar 5, 2016
1 parent 88d2bb3 commit 2e39dd2
Show file tree
Hide file tree
Showing 59 changed files with 1,103 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Sorting/1-tutorial-intro.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char const *argv[])
{
int n, V;
cin >> V >> n;

int temp = 0;
for (int i = 0; i < n; ++i)
{
cin >> temp;
if (temp == V)
{
cout << i << endl;
return 0;
}
}

cout << -1 << endl;

return 0;
}
8 changes: 8 additions & 0 deletions Sorting/1-tutorial-intro.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

$handle = fopen("php://stdin", "r");
fscanf($handle, "%s", $V);
fscanf($handle, "%n", $n);
$ar = explode(' ', trim(fgets($handle)));

echo array_search($V, $ar);
53 changes: 53 additions & 0 deletions Sorting/10-quicksort4.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

function insertionSort($arr){
$times = 0;

for ($i=1; $i < count($arr); $i++) {
$val = $arr[$i];
for ($j=$i - 1; $j >= 0; $j--) {
if ($val < $arr[$j]) {
$arr[$j + 1] = $arr[$j];
$arr[$j] = $val;
$times ++;
} else {
break;
}
}
}

return $times;
}

function quickSort($arr, $start, $end) {
global $times_2;

if ($end - $start < 2)
return;
$p = $arr[$end - 1];
$i = $start;
for ($j=$start; $j < $end; $j++) {
if ($arr[$j] <= $p) {
$temp = $arr[$i];
$arr[$i] = $arr[$j];
$arr[$j] = $temp;
$times_2 ++;
$i ++;
}
}

quickSort($arr, $start, $i - 1);
quickSort($arr, $i, $end);

return $times_2;
}

$handle = fopen("php://stdin", "r");
fscanf($handle, "%d", $t);

$arr = explode(' ', trim(fgets($handle)));

global $times_2;
$times_2 = 0;

echo insertionSort($arr) - quickSort($arr, 0, count($arr)) . PHP_EOL;
22 changes: 22 additions & 0 deletions Sorting/11-countingsort1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

$handle = fopen("php://stdin", "r");
fscanf($handle, "%d", $t);

$arr = explode(' ', trim(fgets($handle)));

$max = max($arr);

$results = [];

for ($i=0; $i <= $max; $i++) {
$results[$i] = 0;
}

foreach ($arr as $a) {
$results[$a] ++;
}

for ($i=0; $i <= $max; $i++) {
echo $results[$i] . ' ';
}
24 changes: 24 additions & 0 deletions Sorting/12-countingsort2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

$handle = fopen("php://stdin", "r");
fscanf($handle, "%d", $t);

$arr = explode(' ', trim(fgets($handle)));
$max = max($arr);
$results = [];

for ($i=0; $i <= $max; $i++) {
$results[$i] = 0;
}

foreach ($arr as $a) {
$results[$a] ++;
}

$results = array_filter($results, function($result) {
return $result != 0;
});

foreach ($results as $key => $value) {
echo str_repeat("$key ", $value);
}
20 changes: 20 additions & 0 deletions Sorting/13-countingsort3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

$handle = fopen("php://stdin", "r");
fscanf($handle, "%d", $t);

$str = [];
$a = [];
$count = array_fill(0, 101, 0);
for ($i=0; $i < $t; $i++) {
fscanf($handle, "%d %s", $a[$i], $str[]);
$count[$a[$i]] ++;
}

for ($i=0; $i < 100; $i++) {
if ($i != 0) {
$count[$i] = $count[$i] + $count[$i - 1];
}

printf("%d ", $count[$i]);
}
38 changes: 38 additions & 0 deletions Sorting/14-countingsort4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <iostream>
#include <string>
#include <vector>

using namespace std;

vector <string> a[100];
string str;
int n, i, j, x;

int main(int argc, char const *argv[])
{
cin >> n;
for (i = 0; i < n / 2; ++i)
{
cin >> x;
cin >> str;
a[x].push_back("-");
}

for (; i < n; ++i)
{
cin >> x;
cin >> str;
a[x].push_back(str);
}

for (i = 0; i < 100; ++i)
{
x = a[i].size();
for (int j = 0; j < x; ++j)
{
cout << a[i][j] << " ";
}
}

return 0;
}
27 changes: 27 additions & 0 deletions Sorting/14-countingsort4.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

$handle = fopen("php://stdin", "r");
fscanf($handle, "%d", $t);

$array = [];
$out = [];

$i = 0;
while ($ar = trim(fgets($handle))) {
$i ++;
list($a, $s) = explode(' ', $ar);
if ($i <= $t / 2) {
$s = '-';
}
$array[(int)$a][] = $s;
}

ksort($array);

foreach ($array as $s) {
foreach ($s as $x) {
$out[] = $x;
}
}

echo implode(' ', $out);
24 changes: 24 additions & 0 deletions Sorting/15-closest-numbers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

$handle = fopen("php://stdin", "r");
fscanf($handle, "%d", $n);

$arr = explode(' ', trim(fgets($handle)));
sort($arr);

$results = [];
$min = $arr[1] - $arr[0];
$mins = [1];

for ($i=2; $i < $n; $i++) {
if ($arr[$i] - $arr[$i - 1] < $min) {
$min = $arr[$i] - $arr[$i - 1];
$mins = [$i];
} else if ($arr[$i] - $arr[$i - 1] == $min) {
$mins[] = $i;
}
}

foreach ($mins as $a) {
echo $arr[$a - 1] . ' ' . $arr[$a] . ' ';
}
9 changes: 9 additions & 0 deletions Sorting/16-find-median.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

$handle = fopen("php://stdin", "r");
fscanf($handle, "%d", $n);

$arr = explode(' ', trim(fgets($handle)));
sort($arr);

echo $arr[floor($n / 2)];
18 changes: 18 additions & 0 deletions Sorting/17-sherlock-and-watson.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

$handle = fopen("php://stdin", "r");
fscanf($handle, "%d %d %d", $N, $K, $Q);

$A = explode(' ', trim(fgets($handle)));

$times = $K % $N;

while ($Q --) {
fscanf($handle, "%d", $a);

if ($a - $times >= 0) {
echo $A[$a - $times] . PHP_EOL;
} else {
echo $A[$a - $times + $N] . PHP_EOL;
}
}
59 changes: 59 additions & 0 deletions Sorting/18-almost-sorted.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <iostream>
#include <vector>
#include <algorithm>

typedef long long LL;

using namespace std;

int main(int argc, char const *argv[])
{
LL n;
vector <LL> origin, sorted, diff;
cin >> n;
for (LL i = 0; i < n; ++i) {
LL x;
cin >> x;
origin.push_back(x);
sorted.push_back(x);
}
sort(sorted.begin(), sorted.end());

for (LL i = 0; i < n; ++i) {
if (origin[i] != sorted[i]) {
diff.push_back(i);
}
}

LL count = diff.size();

if (count == 0) {
cout << "yes" << endl;
return 0;
}

if (count == 2 &&
sorted[diff[0]] == origin[diff[1]] &&
sorted[diff[1]] == origin[diff[0]]) {
cout << "yes" << endl << "swap " << diff[0] + 1 << " " << diff.back() + 1;
return 0;
}

bool reverse = false;
for (LL i = 0; i < count; i ++) {
if (origin[diff[i]] == sorted[diff[count - i - 1]]) {
reverse = true;
} else {
reverse = false;
break;
}
}

if (reverse) {
cout << "yes" << endl << "reverse " << diff[0] + 1 << " " << diff.back() + 1;
} else {
cout << "no" << endl;
}

return 0;
}
41 changes: 41 additions & 0 deletions Sorting/18-almost-sorted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

$handle = fopen("php://stdin", "r");
fscanf($handle, "%d", $n);

$origin = array_map('intval', explode(' ', trim(fgets($handle))));
$sorted = $origin;
sort($sorted);

$diff = [];
for ($i=0; $i < $n; $i++) {
if ($origin[$i] != $sorted[$i]) {
$diff[] = $i;
}
}

$count = count($diff);

//sorted
if ($count == 0) {
echo "yes\n";
} else if ($count == 2 && // swap
$sorted[$diff[0]] == $origin[$diff[1]] &&
$sorted[$diff[1]] == $origin[$diff[0]]) {
printf("yes\nswap %d %d\n", $diff[0] + 1, $diff[1] + 1);
} else if ($count > 2) {
$reverse = false;
for ($i=0; $i < $count; $i++) {
if ($sorted[$diff[$i]] == $origin[$diff[$count - 1 - $i]]) {
$reverse = true;
} else {
$reverse = false;
break;
}
}
if ($reverse) {
printf("yes\nreverse %d %d\n", $diff[0] + 1, $diff[$count - 1] + 1);
} else {
echo "no\n";
}
}
Loading

0 comments on commit 2e39dd2

Please sign in to comment.