The use of the array of data structures is nothing more than adding and removing the operation, on the ordered array and unordered array for these three operations:
First, find
(1) Unordered array finds specific elements, linear lookup:
1 Public Static voidUnsortsearchkey (intArr[],intkey) {2 for(inti = 0; i < arr.length; i++) {3 if(arr[i]==key) {4System.out.println (key+ "+i+" position "indexed in unordered array");5 }6 Else {7 Continue;8 }9 if(i==arr.length) {TenSystem.out.println (key+ "does not exist in an unordered array"); One } A } - -}
(2) ordered array to find specific elements, can be linear or binary lookup (BinarySearch), here we use the binary search array for ascending or descending order, using two points to find code code some different, pit dad
1 Public Static intSortsearchkey (int[] Arrsort,intkey) {2 3 intMax,min,mid; 4Min = 0; 5max = Arrsort.length-1; 6 while(min<=max)7 { 8Mid = (max+min) >>1; 9 if(key>Arrsort[mid])Tenmax = Mid-1; One Else if(key<Arrsort[mid]) AMin = mid + 1; - Else - returnmid; the } - return-min-1; -}
Second, insert
(1) Inserts an element into an ordered array and returns an array after inserting the element:
1 Public Static int[] Insertkey (int[] Arrsort,intKey_insert) {2 //int left=0;3 //int right=arrsort.length-1;4 //int half= (left+right)/2;5 intposition=Sortsearchkey (Arrsort, key_insert);6 if(position>=0){7Arrsort=arrays.copyof (Arrsort, arrsort.length+1);8 for(intx=arrsort.length-1;x>position;x--){9Arrsort[x]=arrsort[x-1];Ten } Onearrsort[position]=Key_insert; A //Displayarr (arrsort); - } - Else{ theArrsort=arrays.copyof (Arrsort, arrsort.length+1); - for(intx=arrsort.length-1;x>=-(position+1); x--){ -Arrsort[x]=arrsort[x-1]; - } +arrsort[-(position+1)]=Key_insert; - //Displaysortarr (arrsort); + } A returnArrsort; at}
(2) Inserting elements in an unordered array (array expansion, inserting to the end of the array)
1 Public Static void insertkey_2 (intint key_insert) {2 arrays.copyof (arr, Arr.length+1); 3 arr[arr.length-1]=Key_insert; 4 Displayarr (arr); 5 }
Third, delete the element
1 Public Static voidDelete_1 (intArr[],intDelsortindexkey) {2 if(delsortindexkey>=arr.length) {3 return;4 }5 for(inti=delsortindexkey;i<arr.length-1;i++){6Arr[i]=arr[i+1];7 }8Arr[arr.length-1]=0;9 Displaysortarr (arr);Ten}
Array of array structures