×

# [closed] Data Structure Tutorial : Array

 4 `If you find any error please comment. I will try to update this post. Array is a collection of homogeneous data elements. It is a very simple data structure. The elements of an array are stored in successive memory location. Array is refered by a name and index number. Array is nice because of their simplicity and well suited for situations where the number is known. Array operation : Traverse Insert Delete Sort Search There are two types of array. One dimensional array and multi dimensional array.One dimensional array This type of array of array represent and strore in linear form. Array index start with zero. Declaration : datatype arrayname[size]; int arr[10]; Input array : for(int i=0; i<10; i++) cin>>arr[i]; We can use store integer type of data to the array arr using above segment. ---------- Traverse : Traversing can easy in linera array. Algorithm: C++ implement : void traverse(int arr[]) { for(int i=0; i<10; i++) cout<=k; i--) { arr[i]=arr[i+1]; } arr[k] = item; n++; } ---------- Deletion : Deletion is very easy on linear array. Algorithm : Deletion(arr, n, k) Here arr is a linear array with n number of items. K is position of elememt which can be deleted. Step 1:Start Step 2: Repeat for i=k upto n [Move the element upword] arr[i]=arr[i+1]; [End of the loop] Step 3: n--; Step 4 : Exit. C++ implementation : void deletion(int arr[], int n, int k) { for(int i=k; iarr[j+1]) then interchange arr[j] and arr[j+1] [End of inner loop] [End of outer loop] Step 4: Exit. C++ implement : void BubbleSort(int arr, int n) { for(int i=0; iarr[j+1]) swap(arr[j],arr[j+1]); } } } Quick Sort: Quick sort is a divide and conquer paradism. In this paradism one element is to be chosen as partitining element . We divide the whole list array into two parts with respect to the partitiong elemnt . The data which are similar than or equal to the partitining element remain in the first part and data data which are greater than the partitioning element remain in the second part. If we find any data which is greater than the partitioning value that will be transfered to the second part., If we find any data whichis smaller than the partitioning element that will be transferred to first part. Transferring the data have been done by exchanging the position of the the data found in first and second part. By repeating this process , we can sort the whole list of data. Algorithm: QUICKSORT(arr, l, h) if l arr[i]) { swap(arr[pivotPosition+1], arr[i]); swap(arr[pivotPosition] , arr[pivotPosition+1]); pivotPosition++; } } return pivotPosition; } void quickSort(int arr[], int low, int high) { if (low < high) { int pi = partition(arr, low, high); quickSort(arr, low, pi - 1); quickSort(arr, pi + 1, high); } } C++ example for simple sorting program with stl function:  #include using namespace std; int main() { int n, arr[100]; cin >> n; for(int i=0; i>arr[i]; } sort(arr, arr + n); for (int i=0; i

### The question has been closed for the following reason "Other" by rashedcs 01 Jan, 11:45

 2 For the deletion algo, wouldn't k-1 be the position of element to be deleted? answered 21 Nov '16, 11:00 0★byteword 26 accept rate: 100% I modified this post. Tnq for finding error. (21 Nov '16, 11:35) rashedcs1★
 2 Nice tutorial. Upvoted. answered 21 Nov '16, 11:53 2.6k●3●20 accept rate: 8%
 0 How do we do a binary search in a matrix or a 2-D array whose columns and rows are sorted? And how to sort a 2-D array? answered 21 Nov '16, 11:52 2.6k●3●20 accept rate: 8% 2 https://www.quora.com/How-do-I-search-in-a-row-wise-and-column-wise-sorted-matrix (21 Nov '16, 11:56) rashedcs1★ Thanks and how do we sort a 2-D array? (21 Nov '16, 11:58)
 0 @rashedcs types of array: one-dimensional and two-dimensional, shouldn't it be multi-dimensional array instead of two-dimensional array, as in dynamic programming sometimes 3D and 4D arrays are also used. answered 21 Nov '16, 11:59 5★srd091 1.5k●1●11 accept rate: 42%
 0 In quick sort, you have written "here's the link". The link is not working. answered 21 Nov '16, 12:01 2.6k●3●20 accept rate: 8% where you have written 'details about quick sort[link]', the link is still not working. (21 Nov '16, 13:25) 2 (22 Nov '16, 13:29) rashedcs1★
 0 @rashedcs Small typo its bubble sort not buble sort or bible sort ... Some suggestions to improve this tutorial , try to explain each function in sorting separately , like provide quicksort(parameters) and partition(parameters) example separately with examples as raw theory is hard to understand for someone who is coming across these algo for first time. Also when you use any built in function try to include its other overloaded versions if any. answered 21 Nov '16, 12:02 3★smsubham 673●2●14 accept rate: 15% Yeap. I always try to improve my tutorial. Tnq for suggestion @smsubham (21 Nov '16, 12:09) rashedcs1★ 2 It is typing mistake. I modified that. (21 Nov '16, 13:20) rashedcs1★
 0 How do we sort a 2-D array by rows and columns both? answered 22 Nov '16, 13:05 2.6k●3●20 accept rate: 8% (22 Nov '16, 13:21) rashedcs1★
 0 You should add the tag algorithm as this tutorial is more about algo's I think. answered 23 Nov '16, 15:06 2.6k●3●20 accept rate: 8% yeap. You are right. (23 Nov '16, 15:09) rashedcs1★

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×773

question asked: 20 Nov '16, 22:26

question was seen: 3,062 times

last updated: 01 Jan, 11:45