Ko‘p o‘lchovli massivlar.
Ko‘p o‘lchovli massivlar ham bir o‘lchovli massivlardek, ikki bosqichda
yoki bir bosqichda aniqlanishi mumkin. Masalan,
int mas[][];// birinchi bosqich
mas=new int[4][5];// ikkinchi bosqich
yoki
int mas[][]=new int[4][5];// bitta bosqich
Bu misolda elementlari butun sonlar bo‘lgan to‘rtta yo‘li va beshta
ustunli ikki o‘lchovli massiv aniqlangan. Ikki o‘lchovli massiv elementlariga
murojaat qilish quyidagicha bo‘ladi:
mas[0][0] – birinchi yo‘l, birinchi ustun
mas[0][4] – birinchi yo‘l, beshinchi ustun
Ikki o‘lchovli massiv elementlariga qiymat berib ham, uni aniqlash
mumkin:
int mas[][]={{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15},{16,17,18,19,20}};
Ikki o‘lchovli massivlarni massiv, ichidagi massiv deb ham qarash
mumkin, ya’ni yuqoridagi massivni quyidagicha ham aniqlasa bo‘ladi:
int mas[][]=new int[4][];
mas[0]=new int[5];
mas[1]=new int[5];
mas[2]=new int[5];
mas[3]=new int[5];
Bu holda ham massiv elementlariga murojaat yuqoridagidek bo‘ladi.
Java ning bu imkoniyatidan foydalanib, ustunlar soni har xil bo‘lgan
massivlarni ham aniqlash mumkin. Masalan,
int ma[][]=new int[4][];
ma[0]=new int[1];
ma[1]=new int[2];
ma[2]=new int[3];
ma[3]=new int[4];
ko‘rinishda massivni ta’riflash, uchburchak ko‘rinishidagi massivni
aniqlaydi. Bu massiv elementlarining indeksi quyidagicha o‘zgaradi:
[0][0]
[1][0] [1][1]
[2][0] [2][1] [2][2]
[3][0] [3][1] [3][2] [3][3]
Shu usulda ko‘p o‘lchovli massivlar aniqlanadi.
Massivni e’lon qilish va joy ajratishni bitta qatorda amalga oshirish mumkin.
int temp_mas[] = new int[12];
1
6
Solishtirish masalalari
|
Java dasturlaash tilida ifodalanish
|
Izoh
|
C++ dasturlaash tilida ifodalanish
|
Izoh
|
Farqlanishi
|
Massivni e’lon qilish
|
[];
int massiv[];
|
|
[];
int massiv[];
|
|
Bir xil
|
Massiv turlari
|
1.Bir o’lchovli massivlar.
int massiv[];
2.Ikki o’lchovli massivlar.
int massiv[][];
3.Uch o’lchovli massivlar.
int massiv[][][];
|
|
1.Bir o’lchovli massivlar.
int massiv[];
2.Ikki o’lchovli massivlar.
int massiv[][];
3.Uch o’lchovli massivlar.
int massiv[][][];
|
|
Bir xil
|
Massivlarni intsializatsiya qilish
|
[]={initsializtor(massiv elementlari kiritiladi)};
1)int massiv[]={1,2,3,5,8,9};
2)int massiv[5]={9,1,0};
3)int massiv[5]={};
|
|
[]={initsializtor(massiv elementlari kiritiladi)};
1)int massiv[]={1,2,3,5,8,9};
2)int massiv[5]={9,1,0};
3)int massiv[5]={};
|
|
Bir xil
|
Saralash funksiyalari
|
Merge sort:
/* Java program for Merge Sort */
class MergeSort
{
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r)
{
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
/* Create temp arrays */
int L[] = new int [n1];
int R[] = new int [n2];
/*Copy data to temp arrays*/
for (int i=0; i
L[i] = arr[l + i];
for (int j=0; j
R[j] = arr[m + 1+ j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarry array
int k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
// Main function that sorts arr[l..r] using
// merge()
void sort(int arr[], int l, int r)
{
if (l < r)
{
// Find the middle point
int m = (l+r)/2;
// Sort first and second halves
sort(arr, l, m);
sort(arr , m+1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
/* A utility function to print array of size n */
static void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i
System.out.print(arr[i] + " ");
System.out.println();
}
// Driver method
public static void main(String args[])
{
int arr[] = {12, 11, 13, 5, 6, 7};
System.out.println("Given Array");
printArray(arr);
MergeSort ob = new MergeSort();
ob.sort(arr, 0, arr.length-1);
System.out.println("\nSorted array");
printArray(arr);
}
}
/* This code is contributed by Rajat Mishra */
|
|
Merge sort:
/* C program for Merge Sort */
#include
#include
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
/* create temp arrays */
int L[n1], R[n2];
/* Copy data to temp arrays L[] and R[] */
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1+ j];
/* Merge the temp arrays back into arr[l..r]*/
i = 0; // Initial index of first subarray
j = 0; // Initial index of second subarray
k = l; // Initial index of merged subarray
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
/* Copy the remaining elements of L[], if there
are any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy the remaining elements of R[], if there
are any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
/* l is for left index and r is right index of the
sub-array of arr to be sorted */
void mergeSort(int arr[], int l, int r)
{
if (l < r)
{
// Same as (l+r)/2, but avoids overflow for
// large l and h
int m = l+(r-l)/2;
// Sort first and second halves
mergeSort(arr, l, m);
mergeSort(arr, m+1, r);
merge(arr, l, m, r);
}
}
/* UTILITY FUNCTIONS */
/* Function to print an array */
void printArray(int A[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", A[i]);
printf("\n");
}
/* Driver program to test above functions */
int main()
{
int arr[] = {12, 11, 13, 5, 6, 7};
int arr_size = sizeof(arr)/sizeof(arr[0]);
printf("Given array is \n");
printArray(arr, arr_size);
mergeSort(arr, 0, arr_size - 1);
printf("\nSorted array is \n");
printArray(arr, arr_size);
return 0;
}
|
|
|
|
// Java program to demonstrate working of Comparator
// interface
import java.util.*;
import java.lang.*;
import java.io.*;
// A class to represent a student.
class Student
{
int rollno;
String name, address;
// Constructor
public Student(int rollno, String name,
String address)
{
this.rollno = rollno;
this.name = name;
this.address = address;
}
// Used to print student details in main()
public String toString()
{
return this.rollno + " " + this.name +
" " + this.address;
}
}
class Sortbyroll implements Comparator
{
// Used for sorting in ascending order of
// roll number
public int compare(Student a, Student b)
{
return a.rollno - b.rollno;
}
}
// Driver class
class Main
{
public static void main (String[] args)
{
Student [] arr = {new Student(111, "bbbb", "london"),
new Student(131, "aaaa", "nyc"),
new Student(121, "cccc", "jaipur")};
System.out.println("Unsorted");
for (int i=0; i
System.out.println(arr[i]);
Arrays.sort(arr, new Sortbyroll());
System.out.println("\nSorted by rollno");
for (int i=0; i
System.out.println(arr[i]);
}
}
Output:
Unsorted
111 bbbb london
131 aaaa nyc
121 cccc jaipur
Sorted by rollno
111 bbbb london
121 cccc jaipur
131 aaaa nyc
|
|
// C++ program to demonstrate descending order sort using
// greater<>().
#include
using namespace std;
int main()
{
int arr[] = {1, 5, 8, 9, 6, 7, 3, 4, 2, 0};
int n = sizeof(arr)/sizeof(arr[0]);
sort(arr, arr+n, greater());
cout << "Array after sorting : \n";
for (int i = 0; i < n; ++i)
cout << arr[i] << " ";
return 0;
}
Output:
Array after sorting :
9 8 7 6 5 4 3 2 1 0
|
|
|
|
|
|
|
|
|
2-
|
|
IV.Foydalanilgan adabiyotlar ro’yhati.
Herbert Schildt : Java™2:A Beginner’s Guide.
Герберт Шилд : Полный справочни по С#.
Герберт Шилд :Базовый курс С++.
Т.А.Павловская : C++.
Страуструп : С++.
Ильдар Хабибуллин : Самоучитель Java.
Петр Карабин Java : Создание интерактивных приложений для Internet.
J o s h u a B lo c h : Effective Java™ Programming Language Guide .
Брюс Эккель : Философия Java 4-е издание.
Internet manbalari:
1.Dasturchi.uz
2.Texnomen.uz
3.Javapoint.com
4.Dasturim.uz
5.Programmis.uz
Do'stlaringiz bilan baham: |