Практикум по программированию. Основы. Массивы. Сортировка массива
From AsIsWiki
(Difference between revisions)
Line 9: | Line 9: | ||
<pre> | <pre> | ||
− | |||
− | |||
− | |||
public class Task19 { | public class Task19 { | ||
Line 17: | Line 14: | ||
public static void main(String[] args) { | public static void main(String[] args) { | ||
− | |||
− | |||
System.out.println(); | System.out.println(); | ||
Line 59: | Line 54: | ||
<pre> | <pre> | ||
+ | // g++ 4.2 | ||
+ | |||
+ | #include <iostream> | ||
+ | |||
+ | using namespace std; | ||
+ | |||
+ | int main() { | ||
+ | |||
+ | cout << "\n Сортировка массива по возрастанию\n"; | ||
+ | cout << "-----------------------------------\n"; | ||
+ | cout << " Массив: "; | ||
+ | |||
+ | srand((int)time(0)); | ||
+ | |||
+ | const int n = 14; | ||
+ | |||
+ | int a[n]; | ||
+ | |||
+ | for (int i = 0; i < n; i++) { | ||
+ | a[i] = rand() % 10; | ||
+ | cout << " " << a[i]; | ||
+ | } | ||
+ | |||
+ | int p; | ||
+ | |||
+ | for (int j = 0; j < n; j++) { | ||
+ | for (int i = 0; i < n - 1; i++) { | ||
+ | if (a[i] > a[i + 1]) { | ||
+ | p = a[i]; | ||
+ | a[i] = a[i + 1]; | ||
+ | a[i + 1] = p; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | cout << "\n Результат:"; | ||
+ | |||
+ | for (int i = 0; i < n; i++) { | ||
+ | cout << " " << a[i]; | ||
+ | } | ||
+ | |||
+ | cout << "\n\n"; | ||
+ | return 0; | ||
+ | } | ||
</pre> | </pre> | ||
Latest revision as of 07:55, 21 April 2015
Contents |
[edit] Java
public class Task19 { private static final int n = 14; public static void main(String[] args) { System.out.println(); System.out.println(" Сортировка массива по возрастанию "); System.out.println("-----------------------------------"); System.out.print(" Массив: "); int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = (int) (10 * Math.random()); System.out.print(" " + a[i]); } int p; for (int j = 0; j < n; j++) { for (int i = 0; i < n - 1; i++) { if (a[i] > a[i + 1]) { p = a[i]; a[i] = a[i + 1]; a[i + 1] = p; } } } System.out.print("\n Результат:"); for (int i = 0; i < n; i++) { System.out.print(" " + a[i]); } System.out.println(); } }
[edit] C++
// g++ 4.2 #include <iostream> using namespace std; int main() { cout << "\n Сортировка массива по возрастанию\n"; cout << "-----------------------------------\n"; cout << " Массив: "; srand((int)time(0)); const int n = 14; int a[n]; for (int i = 0; i < n; i++) { a[i] = rand() % 10; cout << " " << a[i]; } int p; for (int j = 0; j < n; j++) { for (int i = 0; i < n - 1; i++) { if (a[i] > a[i + 1]) { p = a[i]; a[i] = a[i + 1]; a[i + 1] = p; } } } cout << "\n Результат:"; for (int i = 0; i < n; i++) { cout << " " << a[i]; } cout << "\n\n"; return 0; }
[edit] Pascal